I have four 3D matrix like Y= ones(2,2,4) and i want to make a new matrix like YT=zeros(8,8).
I want to take values from 3D "Y" matrix and insert them into 2D "YT" matrix in a following fashion
YT(1,1)=Y(1,1,1)
YT(1,2)=Y(1,2,1)
YT(2,1)=Y(2,1,1)
YT(2,2)=Y(2,2,1)
YT(3,3)=Y(1,1,2)
YT(3,4)=Y(1,2,2)
YT(4,3)=Y(2,1,2)
YT(4,4)=Y(2,2,2)
YT(5,5)=Y(1,1,3)
YT(5,6)=Y(1,2,3)
YT(6,5)=Y(2,1,3)
YT(6,6)=Y(2,2,3)
YT(7,7)=Y(1,1,4)
YT(7,8)=Y(1,2,4)
YT(8,7)=Y(2,1,4)
YT(8,8)=Y(2,2,4)
Can some one suggest me what would be the best way to do this thing automatically? I only assume the values of Y to 1,1,1,1 but in my real case, values of Y are different complex numbers.

 採用された回答

Star Strider
Star Strider 2017 年 3 月 4 日
編集済み: Star Strider 2017 年 3 月 4 日

1 投票

This works:
y = 1:16; % Create Data
Y = reshape(y, 2, 2, 4); % Create Data
RY = reshape(Y(:), 2, []); % Reshape ‘Y’
RC = mat2cell(RY, 2, ones(1,size(RY,2)/2)*2); % Create Submatrices
YT = blkdiag(RC{:}) % Create ‘YT’

4 件のコメント

Adan91h
Adan91h 2017 年 3 月 4 日
Dear Strider,
Thank you very much for your prompt response.
I want to send values automatically to these positions. Your solution works well if we write following command manually
YT=blkdiag(RC{1},RC{2},RC{3},RC{4});
but i want to create a loop with which i can take values from "Y" or "RY" (in your code) and send them automatically to required positions of YT.
I hope this clarify my problem.
BR, Adnan
Star Strider
Star Strider 2017 年 3 月 4 日
The cell array creates a commas-separated list of the kind that blkdiag wants. I changed it my original Answer replacing the ‘YT’ assignment with:
YT = blkdiag(RC{:}) % Create ‘YT’
That does exactly what you want.
(I should have remembered that before. I wasn’t completely awake when I wrote it originally.)
Adan91h
Adan91h 2017 年 3 月 4 日
Thanks once again :). Now its working perfectly. You are great!!!
BR, Adnan
Star Strider
Star Strider 2017 年 3 月 4 日
My pleasure! Thank you for your compliment!
(I apologize for the earlier confusion. I’m UTC-7 here, and wasn’t fully awake then.)

サインインしてコメントする。

その他の回答 (1 件)

John BG
John BG 2017 年 3 月 4 日
編集済み: John BG 2017 年 3 月 4 日

1 投票

Hi Adnan
1.
generating test matrices
Y=randi([-10 10],2,2,4)
YT=zeros(8,8)
YT([1 2],[1 2])=Y(:,:,1)
YT([3 4],[1 2])=Y(:,:,2)
YT([5 6],[1 2])=Y(:,:,3)
2. moving values
[sz1 sz2 sz3]=size(Y)
for k=1:1:sz3
YT([2*k-1 2*k],[2*k-1 2*k])=Y(:,:,k)
end
3. the values of Y and YT can be complex, there's no need to build 2 matrices containing real and imaginary parts.
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG

カテゴリ

ヘルプ センター および File ExchangeMATLAB Mobile についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by