Find the exact multiple of a number by zero padding values.

조회 수: 4 (최근 30일)
CalebJones
CalebJones 2020년 2월 5일
댓글: CalebJones 2020년 2월 5일
Say
A = 13848 x 1
and I want to reshape into a 173(matters a lot) by 80 or 81(this doesn't matter) but that isn't possible as 13848 isn't divible by 173.
So I want a way where it reads A and calculates the next perfect value which is 14013 by zero padding A.
I could just remove 8 from A but I don't loose data but I don't mind zero padding to A towards the end.
  댓글 수: 1
CalebJones
CalebJones 2020년 2월 5일
편집: CalebJones 2020년 2월 5일
I could just manually zero pad A, but I want it to be as automated as possible.

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

채택된 답변

Stephen23
Stephen23 2020년 2월 5일
편집: Stephen23 2020년 2월 5일
Method One: create the output matrix first, then use indexing to add the values:
R = 173;
B = zeros(R,ceil(numel(A)/R));
B(1:numel(A)) = A(:);
Method Two: pad some extra zeros and then remove the superfluous elements:
R = 173;
X = 1:R*fix((1+numel(A))/R)
B = [A;zeros(R,1)];
B = reshape(B(X),R,[]);
  댓글 수: 4
Stephen23
Stephen23 2020년 2월 5일
편집: Stephen23 2020년 2월 5일
Why did you convert this index into a table?:
X = array2table(1:R*fix((1+A)/R))
X is supposed to be a numeric index vector (just like in my answer). Converting it into a table is unlikely to work, and just makes the code pointlessly complex.
Apparently your data are in a table, something that you omitted to mention in your question. I doubt that you have really considered what it means to reshape a table: a table is a container class which contains multiple different variables, it is not a simple array class that can be reshaped, as the variables are not one contiguous array stored in memory. In any case, to reshape you should probably get the appropriate array out of the table first:
CalebJones
CalebJones 2020년 2월 5일
A = size(right_pc,1);
R = 173;
X = 1:R*fix(1+A/R);
B = [right_pc.HbO ; zeros(R,1)];
B = reshape(B(X),R,[]);
Ah yes, SWEEET!
Fixed !
Thanks bud.

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

추가 답변 (1개)

KSSV
KSSV 2020년 2월 5일
편집: KSSV 2020년 2월 5일
A = rand(13848,1) ;
n = length(A) ; % length of A
r = 80 ; % to reshape by this dimension
N = mod(-mod(n,r),r); % get the number of zeros to be appended
A = [A ; zeros(N,1)] ; % append zeros
iwant = reshape(A,r,[]) ; % reshape
  댓글 수: 2
Stephen23
Stephen23 2020년 2월 5일
CalebJones's "Answer" moved here:
getting an error.
n = size(right_pc,1) ; % length of A
r = 80; % to reshape by this dimension
N = mod(-mod(n,r),r); % get the number of zeros to be appended
A = [right_pc(:,1) ; zeros(N,1)] ; % append zeros
iwant = reshape(A,r,[]) ; % reshape
when i tried to run...this
N = mod(-mod(n,r),r);
Index in position 1 exceeds array bounds (must not exceed 173).
I have atteched a mat file as well.
Stephen23
Stephen23 2020년 2월 5일
You have created a variable named mod. Clear that variable from the workspace.

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

카테고리

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