Sorting some data

조회 수: 10 (최근 30일)
Lena Heffern
Lena Heffern 2011년 7월 29일
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
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
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
Walter Roberson 2011년 7월 30일
The code is to find the *beginning* of each run of 2 or more integers.

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


Fangjun Jiang
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
Lena Heffern 2011년 7월 29일
The incident# tells how many groups of 2 numbers there are...
int
int
float
int
float
this would be a 2 #incident
int
int
float
int
float
int
float
would be a 3 #incident
yes, my data is just that annoying
Fangjun Jiang
Fangjun Jiang 2011년 7월 29일
Okay, then Walter's solution should help!

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


Lena Heffern
Lena Heffern 2011년 8월 1일
Almost got it... I just need to figure out how to get rid of my error:
??? Attempted to access data(336607,:); index out of bounds because size(data)=[336606,1].
Code:
i=1;sx=1;sy=1;j=1;xs=zeros((length(data)),(1));ys=zeros((length(data)),(1));m=1;r=0;k=1;
while i+j < length(data)
if ceil(data(i+1,:)) == floor(data(i+1,:)) % is the second line of data a channel number?
n=fix(data(i,:)); % tells how many incident values there are
r=0;
for j=1:2*n % iterates in groups depending on number of incidents, n
% is the channel an x or a y?
if ceil(data(i+j,:)) == floor(data(i+j,:))
if (data(i+j,1) < 16 && data(i+j,1) > -1) % its an x
xo(sx,:) = [data(i+j,:),data(j+i+1,:)];
if ceil(xo(sx,2)) ~= floor(xo(sx,2))
x(sx,:) = [data(i+j,:),data(j+i+1,:)];
xs(m,:)= xs(m,:)+ data(j+i+1,:);
sx=sx+1; j=j+1;
i=i+1;
else
j=j+1;
end
else
y(sy,:) = [data(i+j,:),data(j+i+1,:)];
ys(m,:)= ys(m,:)+ data(j+i+1,:);
j=j+1;
sy=sy+1;
end
else
j=j+1;
end
end
i=i+1; m=m+1;
else
i=i+1;
end
i=i+1;
end

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by