Arrange a set of elements in an array

2 ビュー (過去 30 日間)
Soumyatha Gavvala
Soumyatha Gavvala 2016 年 6 月 28 日
コメント済み: Walter Roberson 2016 年 6 月 29 日
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 件のコメント
Adam
Adam 2016 年 6 月 28 日
So you just want to move the 1 from the start to the end?!
Soumyatha Gavvala
Soumyatha Gavvala 2016 年 6 月 28 日
No, I want to move all the 1's to the 4th position. They all correspond to another set of data and I use the dataset array for which this is one of the column.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2016 年 6 月 28 日
t = reshape(A, 4, []);
t = t([2 3 4 1], :);
A = t(:);
  3 件のコメント
Walter Roberson
Walter Roberson 2016 年 6 月 28 日
Your question is not well defined yet.
If you just want to move the first point to end, as Adam questioned, then
A = [A(2:end); A(1)];
Walter Roberson
Walter Roberson 2016 年 6 月 29 日
The desired outcome is not defined for the case where the array is not a multiple of 4 long. Consider the very last group and suppose it is only 3 long instead of 4, then it would be [1 4 3] for the input, and the 1 needs to move 3 places further down, so it would have to become [4 3 X 1] to match the pattern, but what goes where the X is? Should it become [4 3 2] to match the length? If so then where does the 2 come from? Should it become [4 3 1], moving the 1 to the end of the local group? Maybe, but you don't say what should happen. Perhaps it needs to be left as [1 4 3] if there is not the full set of 4 -- we don't know.

サインインしてコメントする。

その他の回答 (1 件)

John BG
John BG 2016 年 6 月 28 日
編集済み: John BG 2016 年 6 月 29 日
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

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by