Matlab - Multiplication of matrices $A.B$, A being a triangular matrix

35 Views Asked by At

I have this code for the multiplication $A.B=C$, $A(m,p)$, $B(p,n):$

C = zeros(m,n)
for j=1:n
   aux = zeros(m,1)
   for k=1:p
      for i=1:m
         aux(i) = aux(i)+A(i,k)*B(k,j)
      end
   end
   C(:,j) = aux
 end

How can I rewrite this code for $A$, $a_{ij}=0$, $i<j$, in a way that these null elements will not be acessed?

What I tried, but is wrong:

C = zeros(m,n)
for j=1:n
   aux = zeros(m,1)
   for k=1:p
      for i=1:m
         if i<=j
            aux(i) = aux(i)+A(i,k)*B(k,j)
         end
      end
   end
   C(:,j) = aux
 end
1

There are 1 best solutions below

1
On BEST ANSWER

$$C_{ij}=\sum_{k=1}^n A_{ik}B_{kj}$$

When $i < k$, $A_{ik}=0$, hence we can simplify the expression above as

$$C_{ij}=\sum_{\color{red}k=1}^\color{red}i A_{ik}B_{kj}$$

Try to edit your based on the expression above.