How to reconstruct part of a matrix from logical array?

1 次查看(过去 30 天)
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.

采纳的回答

KSSV
KSSV 2016-11-8
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 ;

更多回答(1 个)

Adam
Adam 2016-11-8
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.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by