Clear Filters
Clear Filters

Delete part of a 4D array

9 views (last 30 days)
Suuz HMS
Suuz HMS on 28 Mar 2024
Commented: Rik on 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,:,:,:) = [];
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!

Answers (1)

Rik on 28 Mar 2024
Let's try something:
catch ME
warning('error: %s',ME.message)
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;
Suuz HMS
Suuz HMS on 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 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).

Sign in to comment.


Find more on Matrices and Arrays 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!