How can I multiply a vector (n*1) by each row of a matrix (m*v) to generate m (n*v) matrices?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
John
on 16 Aug 2012
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
John
on 16 Aug 2012
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
on 16 Aug 2012
John
on 16 Aug 2012
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
John
on 16 Aug 2012
Thanks guys, your help is much appreciated. I'll have a bash with this and see how i get on!
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)