Comparing a logical matrix and a numerical matrix?

I want to make a function that takes in as input a matrix (orig) and a logical matrix (mask). If the logical at a certain index is 1, I want the value in that same index for the orig equal to what it is. If it is zero, I want the orig to be the value in that index divided by 4.
function [masked] = maskImage(orig, mask)
For example, if
orig = [45 32 145; 88 33 203; 33 231 40] and
mask = [1 0 1; 1 1 0; 0 1 1], then orig (masked) would become
[45 32/4 145; 88 33 203/4; 33/4 231 40].
Any help is appreciated.

回答 (3 件)

Roger Stafford
Roger Stafford 2014 年 10 月 20 日

2 投票

Assuming 'mask' really is a logical array the same size as 'orig' do:
orig = mask.*orig+(~mask).*orig/4;
Otherwise if 'mask' is actually numerical 1's and 0's do:
orig = (mask==1).*orig+(mask~=1).*orig/4;
dpb
dpb 2014 年 10 月 20 日
編集済み: dpb 2014 年 10 月 21 日

1 投票

masked=orig; % all; only the false entries need modification
masked(~mask)=orig(~mask)/4; % so fixup those...
ADDENDUM
And, as Roger says, if the "mask" is numeric, just turn it to logical...
mask=logical(mask);
first.
Andrei Bobrov
Andrei Bobrov 2014 年 10 月 21 日

0 投票

mask(~mask) = 1/4;
out = orig.*mask;

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

質問済み:

Ray
2014 年 10 月 20 日

回答済み:

2014 年 10 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by