フィルターのクリア

simple question

2 ビュー (過去 30 日間)
Mate 2u
Mate 2u 2012 年 5 月 11 日
I have a array of 1's and -1's. The data is in the format of S =[ 1 1 1 1 1 -1 -1 -1 -1 1 1 1 1]
It is always changing between 1 and -1. Could somebody give me code on how to quantify howmany time it changes from on to the other. Additionally is it possible somebody could give the average of 1's per time 1's occur and average -1's per time -1's occur.

採用された回答

Richard
Richard 2012 年 5 月 11 日
using
ChangesN = find(diff(S))
would demonstrate where the value was changing and then to see how many time this happens:
length(ChangesN)

その他の回答 (2 件)

Win co
Win co 2012 年 5 月 11 日
Hi, I suggest one method below :
Step 1) put all 1 and -1 in a vector, so according to your exemple, we will have :
S1=[1 1 -1 1 -1 1 -1]
Step 2)
count1=0;count2=0;
for i=2:length(S1),
if (S1(i)=-1) and S1(i-1)==S1(i-2)==1),
count1=count1+1;
elsif (S1(i)=1) and S1(i-1)==S1(i-2)==-1),
count2=count2+1;
end
end
Hope il help
Winn
  2 件のコメント
Jan
Jan 2012 年 5 月 11 日
What do you expect as output of "S1(i-1)==S1(i-2)==-1"? Remember, that tha expression is calculated from right to left. Then "S1(i-1)==S1(i-2)" is either TRUE (1) or FALSE (0), but it is never -1.
Win co
Win co 2012 年 5 月 11 日
I wrote the code briefly, it's not helpful anymore if a person put a question and we give him the exact code and then he does a copy-paste. When I wrote the code, I thought he could understand my code.

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


bimal raj
bimal raj 2012 年 5 月 11 日
i=find(S==1);
j=find(S==-1);
n_ptve= length(i);
n_ntve= length(j);
% this may help the 2nd part

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by