How can I multiply a vector (n*1) by each row of a matrix (m*v) to generate m (n*v) matrices?

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?!

 Accepted Answer

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

B = (100*1)
Each row is (1*72),
Thus BK is (100*1)*(1*72) so it should... sorry i should have clarified, i want B multiplied by each row of the big matrix...
The above is just an example for you to see if it is doing what you want by looking at the results and initial inputs. From what you are describing,
K = round(rand(1000,72)*10);
z = round(rand(100,1)*10);
T = cell(size(K,1),1);
for ii = 1:size(K,1)
T{ii} = z*K(ii,:);
end
length(T) % should be 1000 cells
size(T{1}) % each matrix in each cell should be 100-by-72
Yes, this is along the lines of what I need. Basically, the output I need to proceed is the 1000 matrices which are 110-by-72.
I need these as new variables, ideally named something like BK0001-BK1000,
OK, can someone then please explain the easiest way for me to get these 1000 matrices, and in some form so that I can perform further matrix manipulations on them? Apologies for my being a NOOB. :)
Sure, always willing to help a noob! Since all of the matrices are the same size stack them along the third dimension. That way you can refer to them by slice.
M = rand(110,72,1000); %random 3d matrix
Now you can reference the 462nd matrix with
M(:,:,462)
etc. How you get it into this form will depend on how its structured right now.
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
Thanks guys, your help is much appreciated. I'll have a bash with this and see how i get on!

Sign in to comment.

More Answers (0)

Asked:

on 16 Aug 2012

Community Treasure Hunt

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

Start Hunting!