3D array of indices of a 4D array

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 Kommentar

Asvin Kumar
Asvin Kumar am 5 Jan. 2021
Could you share a minimum working example which reproduces your error?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt J
Matt J am 5 Jan. 2021
Bearbeitet: Matt J am 5 Jan. 2021

0 Stimmen

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,:);

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 29 Dez. 2020

Bearbeitet:

am 5 Jan. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by