0

The matrix row are to be increased. Row repetition is allowed and row sequence is not mandatory but expected. The new row size may or may not be divisible by original row size.

A=[1 2 3; 4 5 6; 7 8 9]
B=resize(A,9,3)

This is to be increased to row size of 9 and 11 (two different outputs). Column size remain fixed.

user9003011
  • 306
  • 1
  • 10

2 Answers2

2

If the output must be zero-padded, you can just index the bottom-right corner of the target matrix and assign it a value of 0. There is no need to invoke padarray... Matlab will take care of everything else by itself:

A = [1 2 3; 4 5 6; 7 8 9];

B = A;
B(9,3) = 0;

C = A;
C(11,3) = 0;

If you want to perform this with repetitions, you can use the repmat function, but it can only produce size multiples to the respect of the original matrix... hence some more efforts are required for the second target:

A = [1 2 3; 4 5 6; 7 8 9];

B = repmat(A,3,1);

C = repmat(A,4,1);
C = C(1:11,:);
% or C(12,:) = [];

The last alternative I can figure out require some more manual work (for the copy-over). Let's suppose, for example, that you want your target matrices to be zero-padded once again, then:

A = [1 2 3; 4 5 6; 7 8 9];
[A_rows,A_cols] = size(A);

B = zeros(9,3);
B(1:A_rows,1:A_cols) = A;

C = zeros(11,3);
C(1:A_rows,1:A_cols) = A;

Replacing zeros with ones or NaN will return respectively a one-padded or a NaN-padded matrix instead.

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • It is expected to increase row size with utilization of rows of original matrix. Padding is not the desired approach in tis case. Repmat option seems right. The question is how to obtain new matrix when new row size is not divisible by original row size. – user9003011 Feb 24 '18 at 03:17
  • As shown in my second snippet, you remove the unwanted rows that exceed the wanted matrix dimension: `C = repmat(A,4,1); C(12,:) = [];` or you take the wanted ones, which is maybe simpler: `C = repmat(A,4,1); C = C(1:11,:);`. – Tommaso Belluzzo Feb 24 '18 at 03:36
  • Ok, please edit the answer to the required. – user9003011 Feb 24 '18 at 03:45
  • There is nothing to edit. It's already part of the answer. Read it once again, please. – Tommaso Belluzzo Feb 24 '18 at 03:50
0

Does this look like something that you want?

A=[1 2 3; 4 5 6; 7 8 9];
pad = zeros(6,3);
B= [A; pad]
Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73