How to reconstruct part of a matrix from logical array?
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
KSSV
on 8 Nov 2016
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 ;
0 Comments
More Answers (1)
Adam
on 8 Nov 2016
a( label == 1 ) = someFunctionForFirstSegment( );
a( label == 2 ) = someFunctionForSecondSegment( );
a( label == 3 ) = someFunctionForThirdSegment( );
I wouldn't recommend just hard coding each one in as that isn't extendable of course, but I don't know enough about what your process is to suggest more vectorised ways of doing it, but that is simply how you would update the relevant sections of the vector while keeping the zeros (and other labelled sections you aren't altering) as they are.
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!