How can I multiply a vector (n*1) by each row of a matrix (m*v) to generate m (n*v) matrices?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!