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

 採用された回答

Matt Fig
Matt Fig 2012 年 8 月 16 日
編集済み: Matt Fig 2012 年 8 月 16 日
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 件のコメント

John
John 2012 年 8 月 16 日
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...
Matt Fig
Matt Fig 2012 年 8 月 16 日
編集済み: Matt Fig 2012 年 8 月 16 日
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
John
John 2012 年 8 月 16 日
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,
Sean de Wolski
Sean de Wolski 2012 年 8 月 16 日
John
John 2012 年 8 月 16 日
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.
Matt Fig
Matt Fig 2012 年 8 月 16 日
編集済み: Matt Fig 2012 年 8 月 16 日
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
John
John 2012 年 8 月 16 日
Thanks guys, your help is much appreciated. I'll have a bash with this and see how i get on!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2012 年 8 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by