Sorting some data
이전 댓글 표시
I'm having trouble sorting out some data:
2 % incident#
4 % x (0-15)
59.783
18 % y (16-31)
59.543
3 % incident#
5 % x (0-15)
33.89
6 % x (0-15)
22.462
18 % y (16-31)
58.528
2 % incident#
7 % x (0-15)
59.407
18 % y (16-31)
59.465
2 % incident#
6 % x (0-15)
44.616
17 % y (16-31)
39.891
2 % incident#
19 % y (16-31)
59.886
6 % x (0-15)
59.796
3 % incident#
2 % x (0-15)
13.022
19 % y (16-31)
59.995
3 % x (0-15)
46.00
I'm trying to sort out x's and y's with their corresponding times (under each x or y value). However, some of the times need to add up to ~59, but some of them don't need to add up (x's w/ x's, y's w/ y's depending on incident#). I'm not sure how to sort the data because of the incident#'s being different and having a different number of values after. Plus, the x's and y's aren't always in order. Is there some sort of Boolean operation using integers I can use? In other words, identify when there's 2 integers in sequence (aka the 2, 4, 59.783...) so I can identify when to sort out values?
Sorry if this is extremely confusing, but any help is greatly appreciated.
edit: I'm really trying to avoid having to do a ton of switch/case and if/else statements.
edit: so far I have the following code, but its still not quite working...
i=1;sx=1;sy=1;
while i < length(data)+1
if (data(i+1,1) == 0:1:31) % is the second line of data a channel number?
n=fix(data(i,:)); % tells how many incident values there are
for j=1:1:n % iterates in groups depending on number of incidents
% is the channel an x or a y?
if (data(i+j,:) < 16 && data(i+j,:) > -1)
x(sx,1) = [data(i+j,:),data(1+i+j,:)];
i=i+1; sx=sx+1;
else (data(i+j,:) > 15 && data(i+j,:) > 32)
y(sy,1) = [data(i+j,:),data(1+i+j,:)];
i=i+1; sy=sy+1;
end
end
end
end
답변 (3개)
Walter Roberson
2011년 7월 29일
strfind([false; v(:) == fix(v(:))].', [0 1 1]) - 1
This will return the indices of the beginning of each run of 2 or more integers.
댓글 수: 5
Fangjun Jiang
2011년 7월 29일
Walter, why add a zero at the begining? Based on the data, it doesn't give the correct result.
>> a=strfind([false; v(:) == fix(v(:))].', [0 1 1]) - 1
a =
0 5 12 17 22 27 32
>> b=strfind([v(:) == fix(v(:))]', [ 1 1])
b =
1 6 13 18 23 28 33
Fangjun Jiang
2011년 7월 29일
Or, it is correct. But just no need to subtract 1.
Walter Roberson
2011년 7월 30일
Yes, you are right, no need to subtract 1.
The false at the beginning is to allow a match at the beginning: the strfind() is matching on the _transition_ from No to Yes+Yes, and if there is a Yes+Yes at the beginning then we want to force that to be a transition as well.
Fangjun Jiang
2011년 7월 30일
I don't get it. If I want to find 2 consecutive 1s, it's not going to give me the correct result.
>> strfind(true(1,6),[1 1])
ans =
1 2 3 4 5
>> strfind([false,true(1,6)],[0 1 1])
ans =
1
Walter Roberson
2011년 7월 30일
The code is to find the *beginning* of each run of 2 or more integers.
Fangjun Jiang
2011년 7월 29일
Your data doesn't seem to be consistent. Did you have some typo? I would assume your data should be a repetition of the following 5 lines:
2 % incident#
4 % x (0-15)
59.783
18 % y (16-31)
59.543
If that's the case, you could use reshape() to re-share your data and then it's easy to sort. I modified your data a little bit and got the following result. Is it supposed to be that way?
>> B=reshape(A,5,[])
B =
2.0000 3.0000 2.0000 2.0000 2.0000 3.0000
4.0000 5.0000 7.0000 6.0000 19.0000 2.0000
59.7830 33.8900 59.4070 44.6160 59.8860 13.0220
18.0000 18.0000 18.0000 17.0000 6.0000 19.0000
59.5430 58.5280 59.4650 39.8910 59.7960 59.9950
댓글 수: 3
Lena Heffern
2011년 7월 29일
Lena Heffern
2011년 7월 29일
Fangjun Jiang
2011년 7월 29일
Okay, then Walter's solution should help!
Lena Heffern
2011년 8월 1일
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!