how to expand 2D array?

조회 수: 2 (최근 30일)
roudan
roudan 2021년 3월 12일
댓글: Adam Danz 2021년 3월 12일
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일
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일
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개)

카테고리

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

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by