How to reconstruct part of a matrix from logical array?

1 Ansicht (letzte 30 Tage)
Abhi Ove
Abhi Ove am 8 Nov. 2016
Beantwortet: KSSV am 8 Nov. 2016
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.

Akzeptierte Antwort

KSSV
KSSV am 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 ;

Weitere Antworten (1)

Adam
Adam am 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.

Kategorien

Mehr zu Matrix Indexing 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!

Translated by