How can I multiply a vector (n*1) by each row of a matrix (m*v) to generate m (n*v) matrices?
1 view (last 30 days)
Show older comments
So in my simulation I have a matrix K (1000*72) = 1000 simulations of k And a vector B (100*1).
I need 1000 matrices of Bk
I've turned to MATLAB on the advice of an engineer...
Being completely new to MATLAB (and a coding rookie) I haven't much of a clue where to start...
Please can someone break this down nice and simply for me so I can crack on with the rest of my research?!
0 Comments
Accepted Answer
Matt Fig
on 16 Aug 2012
Edited: Matt Fig
on 16 Aug 2012
If your matrix is 1000-by-72, you cannot multiply this by a 100-by-1. None of the dimensions match up. Each row of your matrix is 1-by-72, so how do you propose to multiply this by a 100-by-1?
The only way I can see that you mean would be something like this:
M = round(rand(5,3)*10)
z = round(rand(4,1)*10)
T = cell(size(M,1),1);
for ii = 1:size(M,1)
T{ii} = z*M(ii,:);
end
Now look at T{1}, T{2}, etc. I this what you mean?
8 Comments
Matt Fig
on 16 Aug 2012
Edited: Matt Fig
on 16 Aug 2012
You have the 1000 matrices stored very efficiently in a cell array. If you need to use them further, simply use them like you would any other matrix. For example:
M = magic(3)
J = [2;3;4]
Result = M*J
But now look what happens if the matrix is stored in a cell array instead:
G = {magic(3)} % Store in cell array G.
Result2 = G{1}*J % You can loop, like G{ii}*J if G has many cells
isequal(Result,Result2) % Same-same
It might help if you read up on cell arrays in MATLAB. They are very useful for doing just what you need to do! I use them all the time for my job to hold large data sets, for example.
doc cell
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!