Asked by Andrew
on 17 Jul 2013

I have two arrays of different size that I am trying to compare. I am therefore trying to extend the smaller array to the bigger one. I have been using the padarray function to add 0 values around the extent of the smaller array. The problem I am encountering is when one matrix has an even number of rows/columns and the other a odd number of rows/columns. For example;

A is a 302x206 logical array

B is a 335x230 logical array

If I use

B = paddarray(B,[16,12])

I am left with a 334x230 logical array. Whilst if I use

B = padarray(B,[17,12])

I am left with a 336x230 logical array.

How can I overcome this problem as the function does not allow decimals to be place in the cell array.

Regards,

Andrew

*No products are associated with this question.*

Answer by dpb
on 17 Jul 2013

Edited by dpb
on 17 Jul 2013

>> help padarray padarray not found. >>

So, don't know what it is; apparently after R2012b.

But

C=logical(zeros(size(B)); % allocate size of larger C(1:size(A,1),1:size(A,2))=A; % place smaller inside

Alternatively, to pad the smaller,

A=[A repmat(zeros(size(A,1),size(B,2)-size(A,2)),1,1)]; % add columns A=[A;repmat(zeros(size(B,1)-size(A,1),size(A,2)),1,1)]; % add rows

NB: the number of rows while adding columns has to be same as shorter and order is significant. If add rows first then have to only have shorter number of columns to maintain consistent sizes in the concatenation.

Show 2 older comments

Andrew
on 19 Jul 2013

Sorry I should have explained the conditions a little better.

PADARRAY() is used to add columns and rows of 0's and more information can be found at http://www.mathworks.co.uk/help/images/ref/padarray.html

I don't quite understand what you mean to pick the start location at the half way point. I have tried this but it is producing errors.

I can add zeros to the row as I have explained above but not to the column when they are odd numbers.

dpb
on 19 Jul 2013

Sorry, I didn't write as precisely as could have--the starting location inside the fully augmented zeros matrix would be

floor((size(larger,1)-size(smaller,1))/2)+1; % row start point floor((size(larger,2)-size(smaller,2))/2)+1; % col start point

Say you have a matrix 7x4 and 12x4; the row to roughly center the 7x4 in a 12x4 would be from 3 thru 9 w/ two blank rows on top and three at the end. The three as the start location would be

floor((size(larger,1)-size(smaller,1))/2)+1; floor((12-7)/2) --> floor(5/2) --> floor(2.5) = 2 + 1 = 3;

If you used ceil() instead of floor() the result would be to place the additional zeros row at the top instead of the bottom.

But, unless you can pad the target size to be an even number larger than the smaller in both directions it's impossible to have exactly the same number of zero rows on both sides of the inset array. (I earlier said both arrays had to be even but of course it's the *difference* in sizes that has to be even in order to be split exactly in half not the size itself).

From the padarray() doc it looks like you could make it work by two calls to add before and after only when it's an odd number to be added total but it seems simpler to me to just place the smaller as outlined above.

Going back to your original you had 335 and 302 as row dimensions -- that's a difference of 13 which is obviously odd so it can only be split 6 and 7. You can put the 6 at the top or the bottom, your choice.

The only way to get an even pad on both sides is to either make the 335 (odd) even by going to 336 or the 302 (even) odd by adding a row to it first or accepting to reduce it by a row.

Either way if it must be balanced exactly the difference has to be an even number since there's no such thing as a half row or column, period.

Andrew
on 22 Jul 2013

I seem to be having a few difficulties with the method you suggested. Thanks for giving the code explaining about entering the code. I am trying to place the smaller matrix inside the larger one but I get errors saying;

"Subscripted assignment dimension mismatch."

My larger matrix is 251x177 and the smaller one 246x168. Then by using the code you gave I tried to insert the matrix at row 3 column 5 using the code;

J(3:size(I,1),5:size(I,2))=I;

I then got the error message listed above. Can you please explain to me what I am doing wrong?

Thanks.

Answer by Sean de Wolski
on 19 Jul 2013

Use `padarray` twice. The first time with the default *'both'* direction the second time with just *'pre'* or *'post'* to add the extra row/col before or after.

padarray(ones(3),[1 2],0,'pre')

Show 1 older comment

Sean de Wolski
on 19 Jul 2013

Completely understandable. I like `padarray` because it scales well for ND:

padarray(rand(3,2,2,3,2,1,1,2),ones(1,8)*2);

Answer by dpb
on 22 Jul 2013

Edited by dpb
on 22 Jul 2013

J(3:size(I,1)+3-1,5:size(I,2)+5-1)=I;

Have to have the subset area the same size as the insert--means have to compensate for the start location (and not forget to subtract one for the endpoint location from the number of elements returned by size())

The first solution used (1,1) as origin so 1:size() worked there as (1:1+size()-1) = 1:size()

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply Today
## 3 Comments

## Dishant Arora (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/82313#comment_160221

you can manually add zeros to B after you have got a 334x230 array i.e:

## Andrew (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/82313#comment_160617

Thanks for your response. This works to add a row of zeros, but I can't seem to get it to add a column of zeros if the columns between the two arrays mismatch

## Dishant Arora (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/82313#comment_160619

You can add a column by omitting the semicolon(;) operator and using proper zeros matrix