Need a method to arrange data ?
Show older comments
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 Comments
Oleg Komarov
on 18 Aug 2012
I don't understand how you go from array a to b.
Jan
on 18 Aug 2012
I agree: The relation between a and b is not sufficiently explained.
Rajan
on 19 Aug 2012
Accepted Answer
More Answers (1)
Azzi Abdelmalek
on 18 Aug 2012
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))]
Categories
Find more on Data Type Identification in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!