How can I avoid a simple loop where matrices are created and then summed?
Infos
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
P is a matrix of size (ny,ny); F0 is a matrix of size (nh,ny); Each row of P multiplies its respective column of F0, creating a matrix, and the resulting matrix of each step is summed to the previous matrix sum.
How can I write the following without the loop so that it is faster?
F1 = 0*ones(nh,ny);
for jj=1:ny
F1 = F1 + F0(:,jj)*P(jj,:);
end
Réponses (2)
Image Analyst
le 24 Déc 2013
0 votes
You can't do it. Let's say ny=3 and nh = 5. So P is a ny*ny or a 3 by 3 matrix. And F0 is a nh*ny or a 5 by 3 matrix. So every row of P has 3 elements (which is the number of columns in P) and every column of F0 has 5 elements (the number of rows in F0). Well, you can't multiply those because they don't have the same number of elements. You can't multiply a 1D vector of 3 elements by a 1D vector of 5 elements. You can't multiply row 1 of P and column 1 of F0, and row 2 of P and column 2 of F0, and row 3 of P and column 3 of F0. I think you made a mistake in what you told us you want done. The only case where it would work is where nh (rows in F0) was the same value as ny (number of columns in P). Is that the case, or were you thinking (incorrectly) that ny and nh could be different?
2 commentaires
You can't multiply a 1D vector of 3 elements by a 1D vector of 5 elements.
You can when it's an outer product,
>> ones(3,1)*ones(1,5)
ans =
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Image Analyst
le 24 Déc 2013
Well, yes, that is true. But when he said "Each row of P multiplies its respective column of F0," I thought he meant a row by a column so, say row 2 of P by column 2 (or any column for that matter) of F0, so you multiply element by element and sum
out22 = P21*F012 + P22*F022 + P23*F032 + P24*F042 + ... PnyF0ny
so F0 must have ny rows, not nh rows like he said. It's also not clear (to me) whether he wants to multiply row 2 of P times all the other columns of F0, or just the second column of F0. What exactly is "its respective column" when you're talking about a particular row?
Cette question est clôturée.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!