How to resize array using a mask

7 次查看(过去 30 天)
I have got an array a = { 1 , 2 , 3 ; 4 , 5 , 6 ; 7 , 8 , 9 }
also have a mask, mask = { 1 ; 0 ; 1 }
If I do a*mask, I want the resulting array = { 1 , 2 , 3 ; 7 , 8 , 9 }
Any suggestions on how to implement this?

回答(3 个)

Walter Roberson
Walter Roberson 2020-9-22
a(logical(cell2mat(mask)),:)

Ameer Hamza
Ameer Hamza 2020-9-22
a = [1 , 2 , 3 ; 4 , 5 , 6 ; 7 , 8 , 9];
b = [ 1; 0; 1];
c = a(b==1, :);

Image Analyst
Image Analyst 2020-9-22
编辑:Image Analyst 2020-9-22
Assuming you're using double arrays...(since I see no need for you to be using cell arrays).
a = [1, 2, 3; 4, 5, 6; 7, 8, 9]
mask = [1; 0; 1]
masked_a = a(logical(mask), :)

类别

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