Convert multi dimensional array to a matrix

Let's say I have A = rand(2,2,2,2). Considering the last two index of the Array A(2,2,x,x) as positions in a matrix such that Matrix(1,1) = A(2,2,1,1) Matrix(1,2) = A(2,2,1,2)..... and so forth.
Ultimately I will end up with a 4,4 matrix in this case.
How can I do this without writing a loop. Please advise.
Update:
I think my question didn't come through correctly. I apologize for it.
Let's have
A(:,:,1,1) =
0.1455 0.8693
0.1361 0.5797
A(:,:,2,1) =
0.5499 0.8530
0.1450 0.6221
A(:,:,1,2) =
0.3510 0.4018
0.5132 0.0760
A(:,:,2,2) =
0.2399 0.1839
0.1233 0.2400
Now I want to have a new Matrix such that
1 2
1 M = [M11 M12;
2 M21 M22];
In my M11 I want A(:,:,1,1)
In my M12 I want A(:,:,1,2) and so forth.
Ultimately I have a 4X4 matrix.
Does it make sense?
Thank you

답변 (1개)

Walter Roberson
Walter Roberson 2017년 2월 17일

0 개 추천

squeeze(A(2, 2, :, :))

댓글 수: 6

Arif Ahmed
Arif Ahmed 2017년 2월 18일
Sorry. That doesn't work
James Tursa
James Tursa 2017년 2월 18일
편집: James Tursa 2017년 2월 18일
Sure it does.
>> A = rand(2,2,2,2)
A(:,:,1,1) =
0.8147 0.1270
0.9058 0.9134
A(:,:,2,1) =
0.6324 0.2785
0.0975 0.5469
A(:,:,1,2) =
0.9575 0.1576
0.9649 0.9706
A(:,:,2,2) =
0.9572 0.8003
0.4854 0.1419
>> squeeze(A(2, 2, :, :))
ans =
0.9134 0.9706
0.5469 0.1419
>> [A(2,2,1,1) A(2,2,1,2); A(2,2,2,1) A(2,2,2,2)] % <-- the explicit elements
ans =
0.9134 0.9706
0.5469 0.1419
You get the same result with the squeeze method and by using the explicit element indexing you specified.
The question is why you think you would end up with a 4x4 matrix. What else about the result are you not telling us? Are you trying to rearrange all of the elements of A somehow? If so, then tell us how.
Arif Ahmed
Arif Ahmed 2017년 2월 18일
편집: Arif Ahmed 2017년 2월 18일
I think my question didn't come through correctly. I apologize for it.
Let's have A(:,:,1,1) =
0.1455 0.8693
0.1361 0.5797
A(:,:,2,1) =
0.5499 0.8530
0.1450 0.6221
A(:,:,1,2) =
0.3510 0.4018
0.5132 0.0760
A(:,:,2,2) =
0.2399 0.1839
0.1233 0.2400
Now I want to have a new Matrix such that
1 2
1 M = [M11 M12;
2 M21 M22];
In my M11 I want A(:,:,1,1)
In my M12 I want A(:,:,1,2) and so forth.
Ultimately I have a 4X4 matrix.
Does it make sense?
James Tursa
James Tursa 2017년 2월 18일
편집: Stephen23 2017년 2월 18일
Do you mean just this?
M = [A(:,:,1,1),A(:,:,1,2);
A(:,:,2,1),A(:,:,2,2)];
Your latest example doesn't seem to agree with the indexing for your original post.
Arif Ahmed
Arif Ahmed 2017년 2월 18일
편집: Arif Ahmed 2017년 2월 18일
Yes. This is what I meant.
Yes, I agree it doesn't agree with my original post. I was thinking of including 2x2 matrix somehow it translated into (2,2,x,x) in writing. I apologize.
shaziah A
shaziah A 2018년 8월 7일
Did you have chance to put them into a matrix? I am also trying to do the same and would really appreciate some help.

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2017년 2월 17일

댓글:

2018년 8월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by