Creating a matrix from a lagrer matrix

조회 수: 2 (최근 30일)
Benedict Egbon
Benedict Egbon 2019년 3월 7일
댓글: Benedict Egbon 2019년 3월 7일
Hello everyone.
Please I need help in automatically dividing the matrix A below into three 5by 1 matrix.
A=[1
2
3
4
5
6
8
9
10
11
12
13
14
15]
Such that the first matrix created will be say
B=[1
2
3
4
5]
Thanks.

채택된 답변

Akira Agata
Akira Agata 2019년 3월 7일
How about the following?
In this case, C{1}, C{2}, ..., C{151} are 200-by-1 matrices each, extracted from original 30194-by-1 matrix.
% Sample 30194-by-1 data
A = rand(30194,1);
% Padding with "nan"
n = 200;
N = numel(A);
A = [A;nan(ceil(N/n)*n-N,1)];
% Divide A into 200-by-1 segments
C = mat2cell(A,repelem(n,ceil(N/n)));
  댓글 수: 2
madhan ravi
madhan ravi 2019년 3월 7일
reshape() is much faster
Benedict Egbon
Benedict Egbon 2019년 3월 7일
Hi Akira.
Thanks for the respone.
This actually worked well for me as desired.
Regards.

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

추가 답변 (1개)

Kevin Phung
Kevin Phung 2019년 3월 7일
A = reshape(1:15,5,3); %reshaped into a 5x3 matrix.
B =A(:,1); %one of the column vectors that you desire
reading up on indexing can help:
  댓글 수: 1
Benedict Egbon
Benedict Egbon 2019년 3월 7일
Thank you very much for the response as this worked very well for this case.
At the moment, I have a 30194 by 1 matrix, and I want to make many 200 by 1 matrices from it.
Any suggestion on how to automate it, instead of doing B =A(:,1), C =A(:,2) etc.
Thanks.

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

카테고리

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