Hi
I create an array with shape of (2, 4), now I'd like to expand the array by 2 times in rows and 2 times in columns so the shape of array will become (4, 8). The values in the original small array will be only copied into a big every 2 rows and every 2 columns. NaN will be used for other locations. For example:
Original array, the values are:
1 2 3 4
5 6 7 8
The new array will be:
1 NaN 2 NaN 3 NaN 4 NaN
NaN NaN NaN NaN NaN NaN NaN NaN
5 NaN 6 NaN 7 NaN 8 NaN
NaN NaN NaN NaN NaN NaN NaN NaN
Then, I'd like to do interpolation to fill NaN in the big array.
Anyone has ideas? Thanks

1 件のコメント

David Hill
David Hill 2021 年 3 月 12 日
Your final matrix is 2x16 not 4x8. Show us the final matrix with the interpolation done that you want.

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

 採用された回答

Adam Danz
Adam Danz 2021 年 3 月 12 日
編集済み: Adam Danz 2021 年 3 月 12 日

0 投票

m = [1 2 3 4
5 6 7 8];
y = reshape([[m;nan(size(m))],nan(size(m,2))],2,[])
y = 2×16
1 NaN 2 NaN 3 NaN 4 NaN NaN NaN NaN NaN NaN NaN NaN NaN 5 NaN 6 NaN 7 NaN 8 NaN NaN NaN NaN NaN NaN NaN NaN NaN
Version 2 after question was updated,
y = reshape([reshape([m(:),nan(size(m(:)))]',size(m,1)*2,[]); nan(size(m).*[2,1])],4,[])
y = 4×8
1 NaN 2 NaN 3 NaN 4 NaN NaN NaN NaN NaN NaN NaN NaN NaN 5 NaN 6 NaN 7 NaN 8 NaN NaN NaN NaN NaN NaN NaN NaN NaN
See comment below for an unpacked version of the line above.

4 件のコメント

roudan
roudan 2021 年 3 月 12 日
Thanks Adam, I made a mistake when writing. The array size should be 4 by 8 instead of 2 by 16. can you revise your answer for me to understand it? Thanks
Adam Danz
Adam Danz 2021 年 3 月 12 日
To unpack that nearly unreadable line of code, work from the inside-out. Here's the same line dissected.
m = [ 1 2 3 4
5 6 7 8];
A = [m(:),nan(size(m(:)))]'
A = 2×8
1 5 2 6 3 7 4 8 NaN NaN NaN NaN NaN NaN NaN NaN
B = reshape(A,size(m,1)*2,[])
B = 4×4
1 2 3 4 NaN NaN NaN NaN 5 6 7 8 NaN NaN NaN NaN
C = [B; nan(size(m).*[2,1])]
C = 8×4
1 2 3 4 NaN NaN NaN NaN 5 6 7 8 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
y = reshape(C,4,[])
y = 4×8
1 NaN 2 NaN 3 NaN 4 NaN NaN NaN NaN NaN NaN NaN NaN NaN 5 NaN 6 NaN 7 NaN 8 NaN NaN NaN NaN NaN NaN NaN NaN NaN
roudan
roudan 2021 年 3 月 12 日
Awesome Adam, you are so considerate to help me unpack each line of code for me to understand it. Thank you. I appreciate it.
Adam Danz
Adam Danz 2021 年 3 月 12 日
Thanks, Yongnuan Liu. Happy to help out.

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2017b

質問済み:

2021 年 3 月 12 日

コメント済み:

2021 年 3 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by