How to devide a matrix (n x 1) into 2 matrixes equally ?
4 views (last 30 days)
Show older comments
Sasha
on 29 Jan 2025
Commented: Walter Roberson
on 29 Jan 2025
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 Comment
Steven Lord
on 29 Jan 2025
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.
Accepted Answer
Walter Roberson
on 29 Jan 2025
A1d = A1(1:ceil(end/2))
2 Comments
Walter Roberson
on 29 Jan 2025
That code already generate the first half.
A1 = rand(140,1);
A1d = A1(1:ceil(end/2));
isequal(A1(1:70), A1d)
A2 = rand(53,1);
A2d = A2(1:ceil(end/2));
isequal(A2(1:27), A2d)
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!