필터 지우기
필터 지우기

reshape a matrix with a divisible of 10....

조회 수: 3 (최근 30일)
Elysi Cochin
Elysi Cochin 2014년 2월 26일
댓글: Elysi Cochin 2014년 2월 26일
i wanted to reshape my a matrix of size 1*N into 10 rows N/10 columns... but when N is not divisible by 10 i get error... how can i rectify the error?? either pad it with zeros to get a number divisible by 10 or get the previous number divisible by 10....
that is if my matrix
A = 1 * 100
i need output B to be 10*10 output
but if A = 1*111....
i get error, so i want to convert
A to 1 * 120 and get B = 10*12 or
change A to 1*110 and get B = 10*11 new output....
please do reply me....

채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 2월 26일
편집: Andrei Bobrov 2014년 2월 26일
r = 10;
out = reshape([A(:);nan(mod(-numel(A),r),1)],r,[])
ADD
out = reshape(A(1:fix(numel(A)/r)*r),r,[])
  댓글 수: 2
Elysi Cochin
Elysi Cochin 2014년 2월 26일
편집: Elysi Cochin 2014년 2월 26일
sir which one should i do... isnt both the out variable is same??? what is the difference??
Andrei Bobrov
Andrei Bobrov 2014년 2월 26일
This is a different solutions, for example:
A =randi(25,1,27)
r = 10;
out1 = reshape([A(:);nan(mod(-numel(A),r),1)],r,[]) % first variant
out2 = reshape(A(1:fix(numel(A)/r)*r),r,[]) % second variant

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

추가 답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 26일
편집: Azzi Abdelmalek 2014년 2월 26일
A=rand(87,1);
n=(10-mod(numel(A),10));
n=n*sign(n);
A(end+1:end+n)=0;
out=reshape(A,10,[])
%or
r=10
m=numel(A);
out=reshape([A;zeros(ceil(m/r)*r-m,1)],r,[])

Iain
Iain 2014년 2월 26일
To pad it with 0's:
if mod(numel(A),10)
A(ceil(numel(A)/10)*10) = 0;
end
B = reshape(A,10,[]);
To pad with NaNs...
if mod(numel(A),10)
A(end+1:ceil(numel(A)/10)*10) = NaN;
end
B = reshape(A,10,[]);
To use the previous divisible:
A((floor(numel(A)/10)*10+1):end) = [];
B = reshape(A,10,[]);

Anuj
Anuj 2014년 2월 26일
Try this, its very simple-
n=10-mod(numel(A),10);
if n==10;
reshape(A,10,numel(A)/10)
else
A=padarray(A,[0,n],'post');
reshape(A,10,numel(A)/10)
end

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by