Rearrangement of vector elements

Hello everyone,
I have a vector that contains 1252459 elements and I need to rearrange these elements so that every 250th and 251nd element become the last elements of the vector. To be more clear, if the initial vector is
b=[1st element; 2nd element;...; 250th element; 251st element; ...; 500th element; 501st element;...;1252459th element]
it has to become
b=[1st element; 2nd element; ... (every element that should not be moved here) ... ; former 250th element; former 251st element; former 500th element; former 501st element; former 750th element ; former 751st element;...]
Thanks in advance.

回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2021 年 4 月 8 日

1 投票

something like this?
a=(1:95)'
b=reshape(a,19,[])
c=b(1:11,:)
d=c(:)
Jan
Jan 2021 年 4 月 8 日
編集済み: Jan 2021 年 4 月 8 日

0 投票

Maybe you want:
b = rand(1252459, 1);
match = false(size(b));
match(1:249:end) = true;
match(2:249:end) = true;
c = b(match);

2 件のコメント

Dc
Dc 2021 年 4 月 8 日
Could you please explain how the rearrangement occurs, since I am not very familiar with using logical arguments? As far as I understand your code, you create a matrix "match" of logical zeros with the same size as b and then assign to the values tha need to be moved a logical 1. I don't quite understand the "c=b(match)" and how it moves the elements in the desired way.
Thanks in advance.
Jan
Jan 2021 年 4 月 8 日
Try it with some smaller data:
x = 11:20
m = false(size(x))
m([2,4,6]) = true
x(m)
m contains FALSE values except for the 2nd, 4th and 6th value, which are true. Using this vector as "logical index", only the elements are replied, which have a TRUE inside the index vector.

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

カテゴリ

ヘルプ センター および File ExchangeMultidimensional Arrays についてさらに検索

質問済み:

Dc
2021 年 4 月 8 日

コメント済み:

Jan
2021 年 4 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by