a 2D into a 3D Array

조회 수: 2 (최근 30일)
akis Lykiardopoulos
akis Lykiardopoulos 2019년 6월 10일
댓글: akis Lykiardopoulos 2019년 6월 11일
Hello people,
I have a a 2D array (is the spectogram of a song with only the real values) and i want to convert it to a 3D. How i will do it? I will take my X Y array (34801 2049) and i will make a new 3D with (9, 2049, 3867) simply by cutting my big array every 9 lines and put them into the 3D in the first frame then for the next i will cut from 10 to 18 the big 2D array and so on.
i wrote this:
begin=1;
end=9;
for i=1:3866
a(:,:,T)=PICT(begin:end,:);
begin=begin+9;
end=end+9;
end
where a is my 3D array and PICT is my 2D array
It does not work for some reason... is there any way in matlab to do it without doing 3 loops?
Hope i was clear
thx in advance for your reply
  댓글 수: 2
Jan
Jan 2019년 6월 11일
"end" is an important Matlab command. You cannot re-use the name as a variable. You should see a corresponding error message.
The loop counter is "i", but you use "T" inside the loop. Is this a typo?
akis Lykiardopoulos
akis Lykiardopoulos 2019년 6월 11일
you are right about this. My apologies. I wrote variables with different names in matlab and here i change their name in order to be more understandable (bad idea).
thx for the reply

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

채택된 답변

Shunichi Kusano
Shunichi Kusano 2019년 6월 11일
Hi. You can do it by reshape and permute like:
PICT = [PICT; zeros(2, 2049)]; % (34803 2049)
a = reshape(PICT.', 2049, 9, 3867); % (2049, 9, 3867)
a = permute(a, [2 1 3]); % (9, 2049, 3867)
hope this helps.
  댓글 수: 1
akis Lykiardopoulos
akis Lykiardopoulos 2019년 6월 11일
thanks a lot for the help !!!

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

추가 답변 (1개)

Jan
Jan 2019년 6월 11일
편집: Jan 2019년 6월 11일
Prefer Shunichi Kusano's solution. But for completeness:
ini = 1;
fin = 9;
a = zeros(9, 2049, 3867);
m = size(PICT, 1);
for k = 1:3867
a(1:fin - ini + 1, :, k) = PICT(ini:fin, :);
ini = min(ini + 9, m);
fin = min(fin + 9, m);
end
  댓글 수: 1
akis Lykiardopoulos
akis Lykiardopoulos 2019년 6월 11일
thx for your time. It helped me understand my mistakes.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by