PadArray with Odd and Even Rows/Colums

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 Commenti

you can manually add zeros to B after you have got a 334x230 array i.e:
B = padarray(B,[16 12]);
B = [zeros(1,230) ; B];
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
You can add a column by omitting the semicolon(;) operator and using proper zeros matrix
B = [zeros(nRows, 1) , B];

Accedi per commentare.

Risposte (3)

dpb
dpb il 17 Lug 2013
Modificato: dpb il 17 Lug 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 Commenti

Thanks for your response.
The reason I was using padarray is so that the values would be added equally around the data as I need to keep the original data in a central location. Using both these methods seems to added the zero values onto the end of the array.
dpb
dpb il 17 Lug 2013
Modificato: dpb il 17 Lug 2013
I don't have PADARRAY() so don't know what it does. It obviously didn't solve your problem though, did it? :)
The arrangement in the middle is a condition you didn't stipulate on the solution before; in that case the first solution is clearly the simpler; just pick the start location as the row/column locations half the distance between the sizes of the two. (floor/ceil will be helpful here depending on which way you want the rounding to go.)
If there's an odd number you'll have to accept an "off-by-one" difference in the size of the padding on one (or both) side(s) or augment the larger by an additional row or/and column as well to make the two arrays sizes both even so the difference is evenly divisible by two.
I suppose if you do the latter then padarray may work--perhaps that's the gist of your initial complaint about the fractional inputs. Clearly there can be no half index, indeed.
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.
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.
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.

Accedi per commentare.

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 Commenti

Yeah, that's what I had mentioned earlier as well; altho seems simpler to me to just place the smaller inside the preallocated zeros() target array.
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);
If one's installation includes, that is... :)

Accedi per commentare.

dpb
dpb il 22 Lug 2013
Modificato: dpb il 22 Lug 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()

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Tag

Richiesto:

il 17 Lug 2013

Community Treasure Hunt

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

Start Hunting!

Translated by