How to set to zero specific elements of a 3D array?

I have a 3D image (array). I want to set to zero all the voxels with numbers 4, 5, 12 and 50-56. Does anyone know how to do this?

Answers (1)

Here if the voxel element is equal to 4, 5 & 50..so on. , all such voxels element will be replaced by 0 (Zero)
data= % #3D Array Element/Image with 3 planes
data(data==4 | data==5 | data==50)=0 %Set the condition accordingly

5 Comments

If I want to set specific voxel elements (e.g., 3,4,5) to one and all the rest to zero? According to your example to set specific elements to 1 should be like the the code below. But how can I set all other elements to zero?
data(data==3 | data==4 | data==5)=1
No, if you refering indices (e.g., 3,4,5)
data(3,4,5)=1
If you refering voxel value (data value), then
data(data~=3 & data~=4 & data~=5)=0
data(data~=0)=1
You can do it in single line too.
Em, I don't think the above lines work. I have a 3D array with voxel values ranging from 0 to 208. I would like to set the voxels with values 52 and 53 to 1 and all the other voxel values to 0.
data=randi([0,60],[10 10])
data = 10×10
4 44 18 59 5 43 32 58 38 6 33 32 48 23 28 43 37 9 7 41 5 23 4 28 33 9 3 60 0 54 44 50 40 12 59 33 55 6 59 2 51 51 48 5 56 22 31 44 28 54 43 30 1 19 9 7 55 56 37 10 52 1 40 17 6 29 56 60 36 60 34 58 44 4 53 44 20 10 4 40 49 14 44 16 27 46 28 17 30 0 24 51 51 32 30 33 20 58 51 21
%..............^ test example only, you can check with 208
data(data~=52 & data~=53)=0
data = 10×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 52 0 0 0 0 0 0 0 0 0 0 0 0 0 53 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
data(data~=0)=1
data = 10×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Here tested with 2D, the logic works on all dimentional data array.

Sign in to comment.

Asked:

on 26 Oct 2022

Commented:

on 27 Oct 2022

Community Treasure Hunt

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

Start Hunting!