필터 지우기
필터 지우기

Need a method to arrange data ?

조회 수: 2 (최근 30일)
Rajan
Rajan 2012년 8월 18일
How can i arrange a array a = [1 2 3 4 3 4 5 3 3 4 2 3 3 4 1 2 3 3 4];
here the numbers 1 ,2 ,...are the order or level in the hierarchy , 1 is top level. 2 is below 1 , 3 is below level 2 and so on.
from first occurence of 1 till next is 1 group of data .
Now i need to arrange array a into array b like this : b =[4 3 5 4 3 3 4 3 2 3 4 3 2 1 3 4 3 2 1];
here tree is like this for data from first 1 till next 1 in array a, i.e.: 1 2 3 4 3 4 5 3 3 4 2 3 3 4
1{
2{
3{
4{
} first 2 elements in array b are 4 ,3 from this part of tree.
}
3{
4{
5{
} next 3 elements in b are 5 ,4, 3 ,and so on..
}
}
3{
}
3{
4{
}
}
}
2{
3{
}
3{
4{
}
}
}
}
  댓글 수: 3
Jan
Jan 2012년 8월 18일
I agree: The relation between a and b is not sufficiently explained.
Rajan
Rajan 2012년 8월 19일
sorry I am not able put this question rightly , I'll try once more a = [1 2 3 4 3 4 5 3 3 4 2 3 3 4 1 2 3 3 4]; let a =[a1 a2]; a1=[1 2 3 4 3 4 5 3 3 4 2 3 3 4]; a2 = [ 1 2 3 3 4] ;
I need b = [b1 b2]; from a1 --> b1 = [4 3 5 4 3 3 4 3 2 3 4 3 2 1]; from a1 --> b2 = [3 4 3 2 1];
To explain relation between a and b , consider a1=[1 2 3 4 3 4 5 3 3 4 2 3 3 4]; I am reading a text file which is as folows: data{
1{
2{
3{
4{
data of level 4;
}
data of 1st level 3; % now b1 = [4 , 3]
}
3{
4{
5{
data of level 5
}
data of level 4
}
data of 2nd level 3; % now b1 = [4 , 3 , 5 , 4 ,3] and soon,
}
3{
data of 3rd level 3;
}
3{
4{
data of level 4;
}
data of 4th level 3;
}
data of level 2 ;
}
2{
3{
data of 1st level 3; %this is first level 3 of second level 2.
}
3{
4{
data of level 4;
}
data of 2nd level 3;
}
data of level 2;
}
data of level 1; }
}%end of data.
Actually array b is the order in which the data of level occurs in the text file.
I hope this time the question is clear.

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

채택된 답변

Matt Fig
Matt Fig 2012년 8월 19일
I am sure there is a more efficient way to do this, but I cannot see it right off.
I = find(a==1);
b = [];
for ii = 1:length(I)
if length(I)>=ii+1
A = a(I(ii):I(ii+1));
else
A = [a(I(ii):end) 1];
end
B = [];
D = [1 diff(A)];
cnt = 1;
while any(D<=0)
L = find(D<=0,1,'first');
L2 = L;
X = A(L);
Y = X+1;
while Y>X
Y = A(L-1);
B(cnt) = Y;
cnt = cnt + 1;
L = L-1;
end
A(L:L2-1) = [];
D = [1 diff(A)];
end
b = [b B];
end
b
  댓글 수: 1
Rajan
Rajan 2012년 8월 19일
Thanks a lot Matt , this is what I was looking for .

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 8월 18일
try this
a = [1 2 3 4 3 4 5 3 3 4 2 3 3 4 1 2 3 3 4];a3=a(3:end);
d=diff(a3);f=find(d<=0);n=length(f);
v=[];k0=1;
for k=1:n
if f(k)>1
v1=a3(k0:f(k))
w=find(or(v1==a(1),v1==a(2)));m=length(w)
if m==1
v=[v v1(1) fliplr(a3(k0+1:f(k)))]
elseif m==2 & f(k)>2
v=[v fliplr(v1(1:2)) fliplr(a3(k0+2:f(k)))]
elseif m==2 & f(k)==2
v=[v fliplr(v1(1:2))]
else
v=[v fliplr(v1)]
end
else
v=[v a3(f(k))]
end
if k<n;k0=f(k)+1,end
end
na3=length(a3);nv=length(v);
if nv<na3
v=[v fliplr(a3(nv+1:end))]
end
v=[v fliplr(a(1:2))]
  댓글 수: 1
Rajan
Rajan 2012년 8월 19일
Hi Azzi , Thanks for this ans , But this is right only when 'a' takes the above values;
If a =[1 1 2 2 3 4 3 3 4];
The above code gives
v = [2 4 3 2 3 4 3 1 1];
but expected value of v is v= [1 2 4 3 3 4 3 2 1];
I think this was my fault because question posted was not clear. Please see the above comment of mine .

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

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by