To generate matrix from an array

15 ビュー (過去 30 日間)
brijesh soni
brijesh soni 2019 年 9 月 24 日
回答済み: Jos (10584) 2019 年 9 月 24 日
Let say I have A=[1; 2; 3; 4; 5; 6; 7; 8] as a single column array. and i want to generate the matrix B and matrix C such that
B=[1 2 3; 2 3 4; 3 4 5; 4 5 6; 5 6 7; 6 7 8]. %if repeation of last two elements of previous row is carried out%
C=[1 2 3 4; 2 3 4 5; 3 4 5 6; 4 5 6 7; 5 6 7 8; ] %if repeation of last three elements of previous row is done% and so on
Please help me to code this.

採用された回答

Adam Danz
Adam Danz 2019 年 9 月 24 日
編集済み: Adam Danz 2019 年 9 月 24 日
Use implicit expansion. (starting r2016b)
B = A(1:end-2) + (0:2); %assuming A is a column vector
C = A(1:end-3) + (0:3); %assuming A is a column vector
  1 件のコメント
brijesh soni
brijesh soni 2019 年 9 月 24 日
Many Thanks!

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

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2019 年 9 月 24 日
More general, using indexing:
A = [1 22 3 44 55 666 7 888 9]
n = 2
B = A(((1:numel(A)-n).' + (0:n)))

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by