How to separate alternate elements from an array?

17 ビュー (過去 30 日間)
Rebeka
Rebeka 2022 年 8 月 27 日
コメント済み: Rebeka 2022 年 8 月 27 日
I have a matrix.
A=[1 2 3 4 5 6 7 8]
I want to separate the alternate elemets from the array.
I tried to do this,
N=[1 2 3 4 5 6 7 8];
Th1=zeros(4,0);
Th2=zeros(4,0);
for i=1:1:8
if mod(i,2)==0;
Th1=N(i);
else
Th2=N(i);
end
end
I am sure solution must be very simple. But, the code seems to save only the last values. Any help?

採用された回答

Stephen23
Stephen23 2022 年 8 月 27 日
A = [1,2,3,4,5,6,7,8];
X = A(1:2:end)
X = 1×4
1 3 5 7
Y = A(2:2:end)
Y = 1×4
2 4 6 8
  1 件のコメント
Rebeka
Rebeka 2022 年 8 月 27 日
As simple as that. Thank you.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by