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 日
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 件のコメント
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 日
You have created a variable named mod. Clear that variable from the workspace.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by