How to fix this error 'Error using xor Matrix dimensions must agree'?
Show older comments
Here is my code
5 Comments
Wine
on 19 Apr 2017
Hi,
I cannot run your Code since it loads some data from your data directory.
Can you maybe post the whole error message with the line details, so I can have a look at the line where the problem occurs?
It is very likely, that you compare two arrays with the “xor” command which have different sizes (different numbers of rows or columns). Maybe you can check the size of your arrays in the brackets after the xor-command. The problematic xor usage should be in the line that is given by the error message.
Cheers,
Ines
waffle
on 27 Apr 2017
Walter Roberson
on 27 Apr 2017
What are size(x7) and size(x9) ?
"So what can I do to make the a9 size =215x550 same as a7?"
This is up to you (the designer of that code): you could take a submatrix, interpolate, pad, permute, or perhaps something else. It depends on your data and your algorithm, which you have told us nothing about.
Answers (4)
Roger Stafford
on 27 Apr 2017
Your a1 and a3 arrays would have different numbers of columns unless the ‘c’ value is exactly half of size(BW,2), so an ‘xor’ operation on them would be undefined:
a1=BW(:,1:c);
a2=BW(:,c+1:end);
.......
a3=fliplr(a2);
a4=xor(a1,a3);
4 Comments
waffle
on 27 Apr 2017
Roger Stafford
on 27 Apr 2017
Edited: Roger Stafford
on 27 Apr 2017
@waffle: Undoubtedly the cause of the error message is that your two arrays, a7 and a9, in the line “a10=xor(a7,a9);” have different sizes along one or more of their dimensions.
With any of matlab’s binary operations on two arrays such as ‘+’, ‘-‘, ‘and’, ‘or’, ‘xor’, and the like, there is a one-to-one matching between the respective elements of the two arrays, and therefore (with certain well-defined exceptions) the respective sizes of these arrays along each of their corresponding dimensions must be equal. That is what your error message is complaining about for arrays a7 and a9. All you have to do is display these respective sizes in the code just before you do the above ‘xor’ operation and you will see the difficulty.
How you fix this will be a problem for you to resolve - we can’t tell you how to do it, because we don’t know what you intended for your algorithm to accomplish.
waffle
on 29 Apr 2017
Roger Stafford
on 30 Apr 2017
That isn’t the right question to ask. You should be asking what the difficulty is in your prior code that has caused a7 and a9 to be different in sizes at that point. That is what needs to be fixed and only you can do that because only you know what it is you are attempting to accomplish with your code.
Jan
on 19 Apr 2017
There are several xor commands in your code. The complete error message shows you, which command is failing. You could use the debugger also: Type this in the command window:
dbstop if error
Then run the code again. What Matlab stops at the error, you can inspect the sizes of the arguments. It is nearly impossible for the readers to fix your code, because you did not insert useful comments in the code. Then we have to guess the intention of the code and all information we have is the failing program. So it is up to you to find out, how to fix the problems.
Image Analyst
on 30 Apr 2017
I could fix your code if you had attached SSM33_orig.jpg. It's not running for a general demo image such as peppers.png and I don't get the error message you get because it doesn't get that far. I get this:
Expected one output from a curly brace or dot indexing expression, but there were 9 results.
Error in test2 (line 36)
c = stats.Centroid(:,1);
Please attach your image if you want people to run your code and help you fix it.
8 Comments
Image Analyst
on 30 Apr 2017
Edited: Image Analyst
on 30 Apr 2017
The problem is here:
a1=BW(:,1:c);
a2=BW(:,c+1:end);
if c is not at the center of the image, those two images will be different sizes. You need to find the bounding box and take the width/2 and subtract that from the centroid to get the same size images.
Something like
width = boundingbox(3);
halfWidth = ceil(width/2);
BWCropped=BW(:,xCenter - halfWidth:xCenter+halfWidth);
Now you can split that into two parts
[rows, columns] = BWCropped);
c = ceil(columns/2);
a1=BWCropped(:,1:c);
a2=BWCropped(:,c:end); % or c+1 if this doesn't work
waffle
on 1 May 2017
Image Analyst
on 1 May 2017
You need to get the bounding box from regionprops. See my tutorial if you don't know how: http://www.mathworks.com/matlabcentral/fileexchange/25157-image-segmentation-tutorial It goes over all that.
waffle
on 3 May 2017
Walter Roberson
on 3 May 2017
If you are taking the regionprops BoundingBox of a 2D image array, then the width is the 3rd column of the BoundingBox an the height is the 4th column.
waffle
on 3 May 2017
Walter Roberson
on 3 May 2017
xCenter = reg.Centroid(1,1) -- that is, it is the x coordinate of the centroid.
Yasir Ghafoor
on 13 Nov 2017
0 votes
Please guide.
1 Comment
Walter Roberson
on 13 Nov 2017
What is size(Rnd_img1) ?
Categories
Find more on Matrix Indexing 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!


