フィルターのクリア

finding and changing values in matrix that satisfies 2 conditions

1 回表示 (過去 30 日間)
Rene Sebena
Rene Sebena 2017 年 2 月 8 日
コメント済み: Rene Sebena 2017 年 2 月 8 日
Hi there, I am new in matlab and try to solve this problem for couple of hours, I have vector which contains various triggers and a lot of zeros:
A=[0;0;0;0;0;0;0;0;2;0;0;1;0;0;0;0;0;0;0;0;1;0;0;0;1;0;0;0;0;0;0;0;4;0;1;0;0;0;0;0;0;0;0;5;1];
what I need to do is to find the second value in matrix A which is bigger than 0 and change it to 100. In this case it is the 12th number, value "1", let's say it is target_1. Then I need to change every second number bigger then 0 and change it to 100, but we have to start counting from target_1.
So the final matrix will be like this:
B=[0;0;0;0;0;0;0;0;2;0;0;100;0;0;0;0;0;0;0;0;1;0;0;0;100;0;0;0;0;0;0;0;4;0;100;0;0;0;0;0;0;0;0;5;100];
Thank you very much ahead for your time and help.

採用された回答

Adam
Adam 2017 年 2 月 8 日
編集済み: Adam 2017 年 2 月 8 日
idx = find( A > 0 );
idx = idx(2:2:end);
B = A
B(idx) = 100;

その他の回答 (1 件)

Alexandra Harkai
Alexandra Harkai 2017 年 2 月 8 日
編集済み: Alexandra Harkai 2017 年 2 月 8 日
idx = find(A); % find indices of nonzero elements in A
B = A; % initially set B to be same as A
B(idx(2:2:end)) = 100;
  1 件のコメント
Rene Sebena
Rene Sebena 2017 年 2 月 8 日
Thank you very much, this is working!

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by