Replacing Specific Elements in an Array

3 visualizaciones (últimos 30 días)
Gwendolyn Williams
Gwendolyn Williams el 3 de Jun. de 2022
Respondida: dpb el 3 de Jun. de 2022
I have a 256x256 matrix with 0s, 1s, 2s, and 3s. Each value (1, 2, 3, and 4) represent one specific region of interest each. I want to separate my matrix into four individual 256x256 matrices - one matrix for each value where all other values are zero. Eg, the first matrix will contain only the 1s, the rest of the matrix will be populated with zeros to create a mask. The second matrix will contain only the 2s, etc.
Once I have my four separate masks, I need each one to be a logical with only zeros and ones.
At present, I have the following:
ROI_stack = niftiread(ROI_stack_dir(1).name) % this gives me a 256x256 uint16 with 0s, 1s, 2s, and 4s
first_mask(ROI_stack ~= 1) = 0;
second_mask(ROI_stack ~= 2) = 0;
third_mask(ROI_stack ~= 3) = 0;
fourth_mask(ROI_stack ~= 4) = 0;
When I run this, my n_mask matrices end up as 1x65536 doubles with only zero values. I'm certain that my ROI_stack does contain non-zero values.
Any help would be appreciated. Thank you in advance.

Respuestas (1)

dpb
dpb el 3 de Jun. de 2022
I'd suggest it's not good practice to create four separate named variables; use a cell array or a 3D array instead.
But, to create each
maskN=(ROI_stack==N);
So,
u=unique(ROI_stack); % get the actual values -- you use both 0-3 and 1-4 above
nu=numel(u); % how many there actually are
maskN=false(size_ROI_stack,nu); % preallocate to logical array with numel(u) planes
for i=1:nu
maskN(:,:,i)=(ROI_stack==u(i));
end

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by