Matrix from an array

조회 수: 1 (최근 30일)
brijesh soni
brijesh soni 2019년 9월 26일
댓글: brijesh soni 2019년 9월 26일
Let say A=1:15 is an array. How to obtain a matrix such that
B = [1 2 3 4 5 6; 4 5 6 7 8 9; 7 8 9 10 11 12; 10 11 12 13 14 15] ; %last n=3 elements of previous row repeats
  댓글 수: 1
Ankit
Ankit 2019년 9월 26일
You need to create a loop in case you want to do it repeated number of times
A=1:15;
B = [A(1:6);A(4:9);A(7:12);A(10:15)];

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2019년 9월 26일
편집: Andrei Bobrov 2019년 9월 26일
A=1:15;
n = 6;
k = 3;
m = numel(A);
B = A((1:k:m - n + 1)' + (0:n-1));
% for old MATLAB < R2016a
B = A(bsxfun(@plus,(1:k:m - n + 1)', (0:n-1)));
  댓글 수: 1
brijesh soni
brijesh soni 2019년 9월 26일
Many thanks !

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bob Thompson
Bob Thompson 2019년 9월 26일
Not sure how to do it without a loop or just defining the indices, but here's something simple.
A = 1:15;
for i = 1:length(A)/3-1
B(i,:) = A((i-1)*3:(i+1)*3);
end

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by