MATLAB: How to reconstruct part of a matrix from logical array

constructlogical?MATLAB

Hi, I have a set of values of random size generated at each iteration, a,
a = [0,0,0,1,2,3,0,0,4,5,6,0,0,7,8,9,10,0,0,0,0,0];
binaryMask = a>0;
label = bwlabel(binaryMask);
b1 = a(a==1); b2 = a(a==2); b3 = a(a==3);
I carry out some tasks on b1,b2 and b3. However the part I am struggling with is to reconstruct the whole data set back with these new values. I would like the 0's to be present in the final column of values such that,
c = [0,0,0,b1,0,0,b2,0,0,b3,0,0,0,0,0];
I feel like I am getting something wrong. Any help?
Many thanks.

Best Answer

You can do some oration on b1,b2 and b3 and replace them using the way you have extracted them. Eg:
a = [0,0,0,1,2,3,0,0,4,5,6,0,0,7,8,9,10,0,0,0,0,0];
binaryMask = a>0;
label = bwlabel(binaryMask);
b1 = a(a==1); b2 = a(a==2); b3 = a(a==3);
% do some operation on b1,b2,b3
b1 = 10*b1 ; b2 = -b2 ; b3 = b3+10 ;
% replace them back
c = a ;
c(a==1) = b1 ;
c(a==2) = b2 ;
c(a==3) = b3 ;
Related Question