Put every 3 data points in matrix A into a separate row in matrix B.

6 ビュー (過去 30 日間)
Curious Mind
Curious Mind 2020 年 6 月 27 日
回答済み: KSSV 2020 年 6 月 27 日
Hi,
I have the following column vector called A:
1
2
3
4
5
6
7
8
9
What I want is a code that will take every 3 data points in A and put it in its own separate row in a new matrix called B. After the first 3 elements or data points in A has been selected and placed in the first row in B, the code should then select the next 3 data points and place them in the 2nd row in B and so on.
e.g., the 1st row in B will be 1,2,3, & 2nd row will be 4,5,6, & 3rd row will be 7,8,9 and so on until every 3 data points in A has been selected and placed in its own row in B.
Please see the attached image.
Thank you.
  3 件のコメント
Curious Mind
Curious Mind 2020 年 6 月 27 日
Thanks @KSSV for your message. I have something like this in mind, B = A(1:3:end) but this will select every 3rd element. I want to select 1 through 3.
dpb
dpb 2020 年 6 月 27 日
No, you don't really want to "select" at all...all you're doing is rearranging.
Think about how and the solution should become apparent.
One line will do the dirty...

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

採用された回答

KSSV
KSSV 2020 年 6 月 27 日
A = 1:9 ;
iwant = reshape(A,3,3)' ;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by