Select every Nth row from number groups

조회 수: 9 (최근 30일)
Mat
Mat 2015년 1월 20일
답변: Jos (10584) 2015년 2월 17일
I have a vector that looks like
a = [1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 1 1 1 1....]
I would like to find the vector location where I would find the first 1, second 1, third 1 and so on, in the trains of 1's in a quickish way.
As an output... I would like new vectors containing only the first 1 in every sequence of 1's
eg
b = [1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0....]
c = [0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0....]
OR
a Matrix containing the locations of each number in a sequence...
b = [1 13 19]
c = [2 14 20]
d = [3 15 21].... and so forth
  댓글 수: 1
Ondrej
Ondrej 2015년 1월 20일
Can you please clarify your question (maybe some example of what you expect)? Do you want the indices of all ones in the vector?, e.g.
a = [1 1 0 1], result = [1 2 4]
?

댓글을 달려면 로그인하십시오.

채택된 답변

Mat
Mat 2015년 2월 17일
I eventually solved this, and am posting my solution... The actual example had some strings containing a mixture of A and B's also, so that's included in this answer.
a='A'
b='B'
aab='AAB'
asandbs=[a;a;a;a;a;a;aab;b;b;b;b;b;b;b;b;a;a;a;a;a;b;b;b;b;a;a;a;a;a;a;a]
str2cell(asandbs)
Blocs=strcmp(asandbs,'B')==1;
Alocs=strcmp(asandbs,'A')==1;
changeovers=~(Alocs+Blocs);
Borders=zeros(size(Blocs,1),1);
for i=2:size(Blocs,1)
if Blocs(i,:)==1
Borders(i,1)=1+Borders(i-1,1)
else
Borders(i,1)=0
end
end
Then repeat the loop for A's

추가 답변 (3개)

Ondrej
Ondrej 2015년 1월 20일
편집: Ondrej 2015년 1월 20일
If the n "trains" of ones have the same length always, then this probably what you want:
result = repmat(find(diff([0 a])==1),n,1)+(0:n-1)'*ones(1,n)
For example:
a = [1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1];
n = 3;
result = repmat(find(diff([0 a])==1),n,1)+(0:n-1)'*ones(1,n)
>> result =
1 13 21
2 14 22
3 15 23
UPDATE:
If the number of your outputs should be equal to the shortest length of the train of ones (in your case 3), then you have to find 'n', e.g.,
tmp = find(diff([0 a])==1);
n = min(find(diff([a 0])==-1) - tmp)+1;
m = length(tmp);
result = repmat(tmp,n,1)+(0:n-1)'*ones(1,m)
  댓글 수: 2
Mat
Mat 2015년 1월 20일
Unfortunately, they are not always the same length (as example)!!
Ondrej
Ondrej 2015년 1월 20일
편집: Ondrej 2015년 1월 20일
If they are not, what would be the next output for your case?
e = [4 NaN 22] ?

댓글을 달려면 로그인하십시오.


Elias Gule
Elias Gule 2015년 2월 17일
편집: Elias Gule 2015년 2월 17일
a = [1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1];
isTake = false;
newMatrix = nan*ones(size(a)); count = 0;
for k = 1 : length(a) val = a(k);
if((val == 1) && ~isTake)
count = count + 1;
newMatrix(count) = k;
isTake = true;
else
isTake = (val == 1);
end
end
newMatrix = newMatrix(~isnan(newMatrix));

Jos (10584)
Jos (10584) 2015년 2월 17일
a = [1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 1 1 1 1]
k = 1 ;
while any(a)
i0 = strfind([0 a],[0 1]) ;
OUT{k} = i0 ;
a(i0) = 0 ;
k = k + 1 ;
end
% OUT{X} holds those indices of A where a 1 occurs as the X-th element in the series of 1's

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by