Delete part of a 4D array
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi! I have a 4D array with dimensions 7501 x 5 x 2 x 5 (data samples x dyads x rower x trials), and for some specific trials, I would like to remove some samples since the data is not gathered correctly.
For example, I would like to remove data samples 2501:7501 of dyad 1, both rower 1 and 2, trial 4. Is this possible?
I already tried
if (dyad == 1 && trial == 4)
data(2501:7501,:,:,:) = [];
end
But that does not seem to work, since then the data from all trials there after are also being removed (so that just 1:2500 remains).
If someone would have a tip, that would be very helpful, thanks!
댓글 수: 0
답변 (1개)
Rik
2024년 3월 28일
Let's try something:
try
data=rand(4,3,5,6);
data(1:2,3,:,:)=[];
catch ME
warning('error: %s',ME.message)
end
Why do you thing this happens? Perhaps it is easier to understand with fewer dimensions:
data = reshape(1:6,2,3)
data(2,3,:) = []
Suddenly it is clear why this doesn't work: you're trying to remove only part of the array. You need to remove full slices. You can remove multiple slices, but not part of slices.
The solution in this case would be to assign NaN, or use a cell, since that can contain empty values.
data(2501:7501,dyad==1,:,trial==4) = NaN;
댓글 수: 2
Rik
2024년 3월 28일
How are you plotting your data? Does any intermediary step compute some summary statistic in a way that doesn't properly deal with NaN values? I didn't hack your computer, so you will have to tell me or think of the underlying cause on your own.
As to your second point: You shouldn't assign an empty cell to elements of your double array. What I suggested is to use a cell array, and then wipe the contents of those cell elements. That has wider implications for the rest of your code (i.e. you will have to change a lot of your code).
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!