PadArray with Odd and Even Rows/Colums
9 views (last 30 days)
Show older comments
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
3 Comments
Dishant Arora
on 19 Jul 2013
You can add a column by omitting the semicolon(;) operator and using proper zeros matrix
B = [zeros(nRows, 1) , B];
Answers (3)
dpb
on 17 Jul 2013
Edited: 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.
5 Comments
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.
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')
4 Comments
dpb
on 22 Jul 2013
Edited: 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()
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!