How to devide a matrix (n x 1) into 2 matrixes equally ?

I have some matrixes, and i need them to be devided into 2 parts equally, and the only part I use is the first part of it. For example, the matrix is A1 that contains 140 values (140 x1), second matrix is A2 contains 53 values (50 x 1), and so on. For matrix A1, I need to take the first 70 values, for matrix A2, i take the first 27 values, and so on. It has not to be in one loop to devide those matrixes all at once. An example only for 1 matrix would be helpful for me.
A1=rand(140,1);
A1d= ...? %is first half part of A1
I hope somebody can help me with my problem.
Thanks in advance.

1 件のコメント

Steven Lord
Steven Lord 2025 年 1 月 29 日
Are you trying to dynamically create variables with numbered names like x1, x2, x3, etc.? You can do this, but should you do this? The general consensus is no. That Discussions post explains why this is generally discouraged and offers several alternative approaches.

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

 採用された回答

Walter Roberson
Walter Roberson 2025 年 1 月 29 日

1 投票

A1d = A1(1:ceil(end/2))

2 件のコメント

Sasha
Sasha 2025 年 1 月 29 日
thanks that's really helpful for me, and do you mind if you tell me the code to get the first half part?
Walter Roberson
Walter Roberson 2025 年 1 月 29 日
That code already generate the first half.
A1 = rand(140,1);
A1d = A1(1:ceil(end/2));
isequal(A1(1:70), A1d)
ans = logical
1
A2 = rand(53,1);
A2d = A2(1:ceil(end/2));
isequal(A2(1:27), A2d)
ans = logical
1

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

その他の回答 (0 件)

カテゴリ

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

製品

リリース

R2018b

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by