Delete part of a 4D array

1 vue (au cours des 30 derniers jours)
Suuz HMS
Suuz HMS le 28 Mar 2024
Commenté : Rik le 28 Mar 2024
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!

Réponses (1)

Rik
Rik le 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
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 commentaires
Suuz HMS
Suuz HMS le 28 Mar 2024
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 le 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).

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by