Map a 2D matrix into 3D using loop for allotment along the 3rd dimension

조회 수: 1 (최근 30일)
I am not able to map a 2D matrix into a 3D, in which I want the particular columns of original 2D matrix to be selected and separated by using 3rd dimension (or in other words breaking a continous data into trials based on columns' selection). I used to get it before, but somehow, I am facing trouble while doing this- it throws an error of "Subscripted dimension error mismatch". But, if I do allotment along the 3rd dimension, size mismatch in other 2 dimensions should not create a problem, this is what I think.
I am now doubtful after getting the dimension mismatch error for it in matlab.
My code is somewhat like this:
% data is 2D matrix of size 7*1298779
for i=1:20
data_trialwise(:,:,i)=data(:,begin(i):end(i)); % begin and end both have 20 elements with values less than the number of columns in data
end
  댓글 수: 2
Turlough Hughes
Turlough Hughes 2019년 12월 6일
I would wager that the way you have generated begin and end do not provide an equal number of indices between them on each iteration. Try subtract the two to see if this is the case. Otherwise can you attach the variables?
Ritz
Ritz 2019년 12월 7일
편집: Ritz 2019년 12월 7일
Yeah, the number of samples are not exactly the same for all 20 trials/loops. Since each trial has 5 sec data with 512 Hz sampling rate, so approx. 2560 samples (between the begin and end of each trial).
But it is not same though for all the trials, as you can see here
task_end-task_begin % for all 20 loops/trials
2562 2568 2563 2565 2560 2561 2572 2559 2570 2560 2568 2567 2557 2568 2562 2558 2568 2559 2559 2561
I am able to get rid of the error by dropping some samples in each, to make them equal size, but is there any other way I can do it?
Thanks a lot !!

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

채택된 답변

Turlough Hughes
Turlough Hughes 2019년 12월 7일
The best way to deal with that is to use a cell array. I've also just renamed 'end' to 'idxend' and 'i' to 'c' as these are predefined in matlab.
clear data_trialwise
for c=1:20
data_trialwise{c,1}=data(:,begin(c):idxend(c));
end
Alternatively, you could preallocate space in data_trialwise so the max(idxend-begin)+1 is assigned to the second dimension, but you would have extra columns of zeros where a given trial had less than 2572 samples.
clear data_trialwise
data_trialwise=zeros(7,max(idxend-begin)+1,20)
for c=1:20
data_trialwise(:,1:(idxend(c)-begin(c)+1),c)=data(:,begin(c):idxend(c));
end
You could also replace the above zeros function with
data_trialwise=NaN(7,max(idxend-begin)+1,20);

추가 답변 (1개)

Matt J
Matt J 2019년 12월 6일
편집: Matt J 2019년 12월 6일
You could try this.
clear data_trialwise
for i=20:-1:1
data_trialwise(:,:,i)=data(:,begin(i):end(i)); % begin and end both have 20 elements with values less than the number of columns in data
end

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by