Different algorithms in MATLAB for multiplication of matrices

30 Views Asked by At

If $A\in \Bbb{R}^{m\times n}$ is a lower triangular matrix with $m<n$ and $B\in \Bbb{R}^{n\times p}$, how can I code this multiplication in a way that the null columns of $A$ will not be accessed? Can it be made by the modification (if i <= k) of one of these codes?

C = zeros(m,n)
for i=1:m
    for j=1:n
        s = 0
        for k=1:p
            s=s+A(i,k)*B(k,j)
        end
        C(i,j) = s
    end
end
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
C = zeros(m,n)
for k=1:p
    for i=1:m
        aux = zeros(1,n)
        for j=1:n
            aux(j) = aux(j)+A(i,k)*B(k,j)
        end
        C(i,:)=C(i,:)+aux
    end
end
1

There are 1 best solutions below

2
On BEST ANSWER

Since $A$ is lower triangular, if $i < k$, then $A_{ik}=0$. Hence we do not need to care about these terms and we only need to add up terms when $k \le i$.

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

Also, note the following $A\in \mathbb{R}^{m \times n}, B\in \mathbb{R}^{n \times p}$, $j$ should take values up to $p$.

The size of $C$ should be $m \times p$.