Multiplying two square matrices by performing vector operations

3 views (last 30 days)
function C=multiply(A,B)
[n,n] = size(A);
[n,n]=size(B);
%C=zeros(n,n);
for j=1:n
for i=1:n
C(i,j)= A(i,:).*B(:,j);
end
end
I have to multiply these two n by n matrices (by performing vector operations), where B is an upper trianguar matrix. But when I run my code, I get that the indices on the left side are not compatible with the size of the right side. But I don't see where I went wrong. Any help please?

Answers (1)

Walter Roberson
Walter Roberson on 19 Sep 2020
When you do algebraic matrix multiplication ("inner product") then you have to sum() the result of the individual multiplications.
You will find that you also need to transpose one of the two vectors.
  3 Comments
Walter Roberson
Walter Roberson on 19 Sep 2020
It is not possible to meaningfully measure flops on any modern computer. The introduction of the MIPS R8000 in 1992 was pretty much the end of the era for being able to measure flops. The Sun SPARC "SuperScalar" was already not easy to measure flops on, but it was possible to assign some meaning to flops on it; the speculative execution of the MIPS R8000 broke down the last pretense that flops were still meaningful.
Walter Roberson
Walter Roberson on 19 Sep 2020
C(i,j)+sum(A(i,:).*B(:,j)')
The addition of the C(i,j) is not useful at that point. By examining your code you can see that C(i,j) is always going to be 0 at that point in the code.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!