Info

この質問は閉じられています。 編集または回答するには再度開いてください。

I want element (3,2) from a matrix with size (4,4) to try the values of the first column from another matrix with size (3,3) one by one and at each time produce the new matrix. How can I do this ?

1 回表示 (過去 30 日間)
Adi Nor
Adi Nor 2017 年 5 月 9 日
閉鎖済み: Stephen23 2017 年 5 月 10 日
For example: If I have matrix A and Matrix B:
A =
7 4 1
4 5 6
3 6 9
B =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
I want element (3,2) from matrix B with size (4,4) to try the values of the first column from matrix A with size (3,3) one by one and at each time produce the new matrix. So, the output will be:
B =
0 0 0 0
0 0 7 0
0 0 0 0
0 0 0 0
B =
0 0 0 0
0 0 4 0
0 0 0 0
0 0 0 0
B =
0 0 0 0
0 0 3 0
0 0 0 0
0 0 0 0
How can I do this ?
  1 件のコメント
Stephen23
Stephen23 2017 年 5 月 10 日
Duplicate:
https://www.mathworks.com/matlabcentral/answers/339458-what-s-the-solution

回答 (1 件)

Geoff Hayes
Geoff Hayes 2017 年 5 月 9 日
Mahmoud - do you want to keep the updated B at each iteration, or just use it and then dispose of it?
for k=1:size(A,1)
B = zeros(4,4);
B(2,3) = A(k,1);
% do something with B
end
In the above, we iterate over each row in the first column of A and set the appropriate element of B with it.
Note how we use B(2,3) to access the element in the second row and third column of B (rather than the B(3,2) that you mention).
  2 件のコメント
Adi Nor
Adi Nor 2017 年 5 月 9 日
編集済み: Adi Nor 2017 年 5 月 9 日
If I want also B(4,2) to take values from 2nd column in matrix A beside B(3,2) takes values of the 1st column of matrix A .. How can I do this ?
Geoff Hayes
Geoff Hayes 2017 年 5 月 10 日
Which value in the second column of A? You should be able to modify the above so that you can accomplish that task.

Community Treasure Hunt

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

Start Hunting!

Translated by