Hi.I want to multiply n matrices with k,k+1 dimension and the problem is that if I test the code on paper it seems right but written on matlab well it's a little bit different. here is the code :
2 views (last 30 days)
Show older comments
Hi. I want to multiply p matrices with k,k+1 dimension and the problem is that if I test the code on paper it seems right, but written in matlab, well, it's a little bit different. Here is the code :
for k=1:p-1
for i=1:size(A{k+1},2)
R(1,i)=0;
for j=1:size(A{k},2)
R(1,i)=R(1,i)+A{k}(1,j)*A{k+1}(j,i);
A{k+1}(1,i)=R(1,i);
end
end
end
R
Would you please take a look and tell me, if you can, what's wrong?
Because if I check R with A1*A2*A3 for example they are not the same. And they should...
Thank you.
4 Comments
Roger Stafford
on 16 Nov 2016
If A1 and A2 each have k rows and k+1 columns, then A1*A2 is an invalid matrix multiplication, because A1 must have the same number of columns as A2 has rows. Of course the same difficulty pertains to A1*A2*...*An when they each are k by k+1 in size.
Answers (1)
Roger Stafford
on 15 Nov 2016
All you are going to get with this computation is the first row of the product of these A{...} matrices. The result will therefore of course not agree with the full product. To do the for-loop type computation you are using, you would need to have them four deep instead of three to get all rows.
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!