フィルターのクリア

How to write this simple script correctly?

1 回表示 (過去 30 日間)
Rita
Rita 2015 年 8 月 11 日
コメント済み: Rita 2015 年 8 月 11 日
Hi I have
s=[1 0 1;0 0 0];
b=[2 3 4];
I would like to have this as a result
c=[nan 3 nan;2 3 4]
I mean replace 1 by nan and o by b.
for r= 1:3
s_ind = find(s(:,r) == 1);
s(s_ind)=nan;
m(:,r)=s
a_ind = find(s(:,r) == 0);
% I don't know how to replace for 0 from matrix b??
end
any help would be appreciated in advance.

採用された回答

Stephen23
Stephen23 2015 年 8 月 11 日
編集済み: Stephen23 2015 年 8 月 11 日
Simply using logical indexing:
>> s = [1,0,1;0,0,0];
>> b = [2,3,4];
>> out = repmat(b,size(s,1),1);
>> out(s==1) = NaN
out =
NaN 3 NaN
2 3 4
  1 件のコメント
Rita
Rita 2015 年 8 月 11 日
Thanks for your quick responses.

サインインしてコメントする。

その他の回答 (1 件)

Star Strider
Star Strider 2015 年 8 月 11 日
You can use logical indexing, although you need two separate steps to assign the elements of ‘b’ and ‘NaN’ to ‘c’ because you have two separate conditions.
One possibility:
s=[1 0 1;0 0 0];
b=[2; 3; 4];
mb = repmat(b', 2, 1); % Create Matrix Matching ‘s’ From ‘b’
c = s; % Create ‘c’
c(~c) = mb(~c); % Assign Elements Of ‘mb’ To Zero Elements Of ‘c’
c(c==1) = NaN; % Assign ‘NaN’ To ‘c’ = 1

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by