Delete part of a 4D array
3 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Rik
on 28 Mar 2024
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 Comments
Rik
on 28 Mar 2024
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).
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!