필터 지우기
필터 지우기

Split a row into several rows

조회 수: 29 (최근 30일)
Olu adroit
Olu adroit 2016년 1월 14일
댓글: Olu adroit 2016년 1월 14일
Hi all, I am trying to split a single-row matrix say for example
A = [1,2,3,4,17,18,19,20,33,34,35,36,49,50,51,52,65,66]
into a multiple-row matrix B, the maximum number of columns in B being 5. I would expect to get something like
B = [1,2,3,4,17;18,19,20,33,34;35,36,49,50,51;52,65,66]
I tried using the reshape function as below but it's giving an error saying
Product of known dimensions, 5, not divisible into total number of elements, 18
Please any suggestion would be appreciated.
%
% I would like to get
% B = 1 2 3 4 17
% 18 19 20 33 34
% 35 36 49 50 51
% 52 65 66
%
% code
A = [1,2,3,4,17,18,19,20,33,34,35,36,49,50,51,52,65,66];
B = reshape(A, 5, []);

채택된 답변

Stephen23
Stephen23 2016년 1월 14일
편집: Stephen23 2016년 1월 14일
A matrix cannot have gaps or missing values: every row must be the same length, just as every column must too. So you will have to join some values onto A before reshaping it:
>> N = 5;
>> reshape([A,nan(1,N-mod(numel(A),N))],[],N)
ans =
1 17 33 49 65
2 18 34 50 66
3 19 35 51 NaN
4 20 36 52 NaN
  댓글 수: 2
Olu adroit
Olu adroit 2016년 1월 14일
Thanks for this Steve. What if I want to pad with 0 and not NaN? I subsequently exported the multi-row matrix as a csv file into Abaqus software, which is giving me error message because of the exported NaN. Thanks in advance.
Olu adroit
Olu adroit 2016년 1월 14일
ok i got it now. I substituted NaN with zeros. Thanks

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

추가 답변 (1개)

Ilham Hardy
Ilham Hardy 2016년 1월 14일
The error message indicates the error very clear. What will be the dimension of the B matrix then? 5x4.9 matrix?
Unless you pad the A matrix beforehand with
A = [A,NaN,NaN];
the reshape command will not works (obviously).

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by