Binary mask operation: vector into nonzero part of mask in 1 statement?

조회 수: 3 (최근 30일)
Jurgen
Jurgen 2012년 12월 11일
I have a logical matrix MASK and a vector VEC with real numbers (uint8 mostly). nnz(MASK) equals length(VEC).
I create a new matrix NEW where the logical 1's in MASK are replaced by the values in VEC--in sequential order:
NEW = MASK;
NEW(NEW>0) = VEC;
Can I do this with 1 statement? Because in practice I want to replace NEW with VEC since I dont need VEC anymore after the operation, and don't want to pile up variables. MASK should remain untouched.
Thanks

답변 (1개)

Image Analyst
Image Analyst 2012년 12월 11일
I think you can just do:
VEC(~MASK) = 0;
I think this will do the same thing - set VEC to zero where MASK is false, and VEC stays VEC where MASK is true.
By the way, you can use clear('variableName') to get rid of variables you don't need anymore.
  댓글 수: 4
Image Analyst
Image Analyst 2012년 12월 12일
편집: Image Analyst 2012년 12월 12일
It looks like in your code demo that vec will need to get resized, so no, there's no way to do it in a single assignment. Use clear('VEC') to get rid of VEC after you've created NEW.
Jurgen
Jurgen 2012년 12월 12일
Thanks, in the end I just made a function that takes the mask and vector and spits out a substituted image. If you cant beat em, join em :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Author Block Masks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by