Filter löschen
Filter löschen

PadArray with Odd and Even Rows/Colums

6 Ansichten (letzte 30 Tage)
Andrew am 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.
  3 Kommentare
Andrew am 19 Jul. 2013
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
Dishant Arora am 19 Jul. 2013
You can add a column by omitting the semicolon(;) operator and using proper zeros matrix
B = [zeros(nRows, 1) , B];

Melden Sie sich an, um zu kommentieren.

Antworten (3)

dpb am 17 Jul. 2013
Bearbeitet: dpb am 17 Jul. 2013
>> help padarray
padarray not found.
So, don't know what it is; apparently after R2012b.
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 Kommentare
dpb am 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((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 am 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;
I then got the error message listed above. Can you please explain to me what I am doing wrong?

Melden Sie sich an, um zu kommentieren.

Sean de Wolski
Sean de Wolski am 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 Kommentare
dpb am 19 Jul. 2013
If one's installation includes, that is... :)
Sean de Wolski
Sean de Wolski am 19 Jul. 2013

Melden Sie sich an, um zu kommentieren.

dpb am 22 Jul. 2013
Bearbeitet: dpb am 22 Jul. 2013
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()


Community Treasure Hunt

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

Start Hunting!

Translated by