3D array of indices of a 4D array
Show older comments
Hello everyone. I'm new to this forum.
I want to mask a 3D volume that changes over time (i.e. a 4D array) with a 3D binary mask, where 1 are in the positions that I want to keep and 0 the positions that I'm not interested in. Visually, they look more or less like this:

What I was trying is this:
% ´V´ is a width x height x depth x timePoints array, and ´mask´ is a width x height x depth array.
maskTime = repmat(mask,[1 1 1 timePoints]); % Repeat mask over time (size(maskTime) = width x height x depth x timePoints)
[X,Y,Z,T] = ind2sub(size(maskTime),find(maskTime==1)); % X,Y,Z and T are the positions in every dimension where there's a 1
maskedVolume = V(X,Y,Z,T);
However, when I run the code, I get the error: 'Requested array exceeds the maximum possible variable size.'
In my case, I have that the variables ´X´,´Y´, ´Z´ and ´T´ are:


And they contain all the indices where there's a 1. For example, in the case ´X´:

Here, we can see all the indices. Do you know how to fix this problem? I'm interested in avoiding for loops.
Thank you in advance.
1 Comment
Asvin Kumar
on 5 Jan 2021
Could you share a minimum working example which reproduces your error?
Answers (1)
It should be sufficient just to do
maskTime=logical(maskedTime);
maskedVolume=V.*maskTime;
or possibly you were looking for
maskTime=logical(maskedTime);
maskedVolume=reshape(V,[],timePoints);
maskedVolume=maskedVolume(maskTime,:);
Categories
Find more on Operators and Elementary Operations 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!