What's the solution?

조회 수: 1 (최근 30일)
Adi Nor
Adi Nor 2017년 5월 9일
편집: Adi Nor 2017년 5월 9일
For example: If I have matrix A and Matrix B:
A =
7 4 1
4 5 6
3 6 9
>> B = zeros(4,4)
B =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
I want 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 ?

채택된 답변

Stephen23
Stephen23 2017년 5월 9일
편집: Stephen23 2017년 5월 9일
Here is one easy way, by generating one 3D array B and allocating all values of A in one go:
>> A = [7,4,1;4,5,6;3,6,9];
>> B = zeros(4,4,numel(A));
>> B(2,3,:) = A(:);
and testing each page of B, shows that it has all of the correct matrices:
>> B(:,:,1)
ans =
0 0 0 0
0 0 7 0
0 0 0 0
0 0 0 0
>> B(:,:,2)
ans =
0 0 0 0
0 0 4 0
0 0 0 0
0 0 0 0
>> B(:,:,3)
ans =
0 0 0 0
0 0 3 0
0 0 0 0
0 0 0 0
>> B(:,:,4)
ans =
0 0 0 0
0 0 4 0
0 0 0 0
0 0 0 0
etc
  댓글 수: 2
Adi Nor
Adi Nor 2017년 5월 9일
편집: Adi Nor 2017년 5월 9일
I can't deal with the 3D matrix after that. Is there any solution where B is still 2D matrix? and with this solution element(3,2) has all values of matrix A. I want element (3,2) to take the values from the 1st column in matrix A only.
Stephen23
Stephen23 2017년 5월 9일
편집: Stephen23 2017년 5월 9일
"I can't deal with the 3D matrix after that"
Yes, you can: I showed you how to get the 2D matrices out of it. Have another look at all of those matrices in my answer. All of them are 2D. You can trivially loop over the third dimension of B to give you exactly what you asked for:
for k = 1:size(B,3)
M = B(:,:,k);
... your code dealing with M.
end
"I want element (3,2) to take the values from the 1st column in matrix A only."
Easy:
B = zeros(4,4,size(A,1));
B(2,3,:) = A(:,1);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by