Delete part of a 4D array

조회 수: 5 (최근 30일)
Suuz HMS
Suuz HMS 2024년 3월 28일
댓글: Rik 2024년 3월 28일
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!

답변 (1개)

Rik
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
Warning: error: A null assignment can have only one non-colon index.
Why do you thing this happens? Perhaps it is easier to understand with fewer dimensions:
data = reshape(1:6,2,3)
data = 2×3
1 3 5 2 4 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
data(2,3,:) = []
A null assignment can have only one non-colon index.
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
Suuz HMS
Suuz HMS 2024년 3월 28일
Thank you for your respons!
When I use the option
data(2501:7501,1,:,4) = NaN;
it seems to work and the right samples get replaced by NaN, but when I try to plot the data, nothing comes up.
When I use a cell
data(2501:7501,1,:,4) = {};
I get the warning:
Conversion to double from cell is not possible.
Do you know how this can be solved?
Rik
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by