I need help in splitting an array

The array I have-
A=[ 2 3 , 4 5 , 8 12 , 5 6 ]
The output I need-
C= [ 2 3 , 4 5 ]
D= [ 8 12 , 5 6 ]

6 件のコメント

the cyclist
the cyclist 2021 年 1 月 26 日
What is the size of A? Do you mean it is a 1x8, or a 4x2? Or something else?
Similarly are C and D supposed to be 1x4, or 2x2?
(It is unclear from your noncanonical use of the commas.)
Stephen23
Stephen23 2021 年 1 月 26 日
Very simple:
A = [ 2 3 , 4 5 , 8 12 , 5 6 ]
A = 1×8
2 3 4 5 8 12 5 6
B = A(1:4)
B = 1×4
2 3 4 5
C = A(5:8)
C = 1×4
8 12 5 6
Walter Roberson
Walter Roberson 2021 年 1 月 26 日
Stephen assumed that the rule is that the array is fixed length and that it is to be divided into two pieces of equal length. Those are reasonable assumptions.
My solution made different assumptions, that the input is to be divided into groups of 4 values and that as many different variables as needed should be used. Those might sound like odd assumptions at first, but we have learned over time that people who do not simply do the obvious array indexing usually have a broader problem in mind so I wrote for one of the more common broader problems.
If only there were some way for people who post questions to be more specific about what they want to do...
the cyclist
the cyclist 2021 年 1 月 26 日
lol ... if only
Sikder Meaghi
Sikder Meaghi 2021 年 1 月 28 日
Thank you very much for the solution. I already found a different approach to it but i shall definately try this too
Walter Roberson
Walter Roberson 2021 年 1 月 28 日
If only there were some way for people who post questions to be more specific about what they want to do...

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

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 1 月 26 日

2 投票

The following code is only valid for A up to 96 entries, after which it will run out of single-character variable names to assign the rows of 4 values into.
A=[ 2 3 , 4 5 , 8 12 , 5 6 ]
A = 1×8
2 3 4 5 8 12 5 6
whos
Name Size Bytes Class Attributes A 1x8 64 double
q7ta90 = buffer(A,4);
for ilil1li = 1 : size(q7ta90, 2)
eval( char([66+ilil1li,61,mat2str(q7ta90(:,ilil1li)),46,39,59]) )
end
clear q7ta90 ilil1li
whos
Name Size Bytes Class Attributes A 1x8 64 double C 1x4 32 double D 1x4 32 double
C
C = 1×4
2 3 4 5
D
D = 1×4
8 12 5 6
Do you need the commas as part of the output? If so then the output cannot be numeric.

1 件のコメント

Sikder Meaghi
Sikder Meaghi 2021 年 1 月 28 日
Thank you very much for the solution. I already found a different approach to it but i shall definately try this too

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

カテゴリ

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

質問済み:

2021 年 1 月 26 日

コメント済み:

2021 年 1 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by