Replacing Specific Elements in an Array
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Antworten (1)
dpb
am 3 Jun. 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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!