Arrange a set of elements in an array
Afficher commentaires plus anciens
Hello,
So i have an array that goes like this:
A=
[1
4
3
2
1
4
3
2
1
4
3
2
1
4
3
2
.
.
.
]
I want to arrange it as
A=
[4
3
2
1
4
3
2
1
4
3
2
1
4
3
2
1
4
3
2
1
]
How can I do this?
2 commentaires
Adam
le 28 Juin 2016
So you just want to move the 1 from the start to the end?!
Soumyatha Gavvala
le 28 Juin 2016
Réponse acceptée
Plus de réponses (1)
Soumyatha
have you tried a circshift?
A = 1.00
4.00
3.00
2.00
1.00
4.00
3.00
2.00
1.00
4.00
3.00
2.00
1.00
4.00
3.00
2.00
circshift(A,-1)
=
4.00
3.00
2.00
1.00
4.00
3.00
2.00
1.00
4.00
3.00
2.00
1.00
4.00
3.00
2.00
1.00
or perhaps you mean that there may be any amount of each number:
L=uint64(randi([1 4],randi([10 27],1,1),1)) % this is just a test matrix
h1=histogram(L)
Val=h1.Values;
L0={};
for k=1:1:length(Val) L1=k.*ones(1,Val(k)); L0=[L0;L1]; end
% size matters: measure max chain length
dist1=zeros(1,length(Val)); for k=1:1:length(Val) dist1(k)=numel(L0{k}); end
L2=zeros(length(Val),max(dist1))
for k=1:1:length(Val) L2(k,[1:numel(L0{k})])=L0{k}; end
L3=flip(L2);
L4=uint64(nonzeros(L3(:)));
If you find this answer of any help solving your question,
please mark my answer as accepted
thanks in advance
John
Catégories
En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!