You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Matrix dimension must agree to use .*
1 view (last 30 days)
Show older comments
I debugged my code and checked my workspace variables, and when I loaded in the two images their numbers were identical (matrix dimensions, their matrix dimensions after I manipulated them) yet when I use .* it still says an error because .* requires identical matrix dimensions, when, Q.E.D., that's what I have! Suggestions?
Cheers,
Neo Cornel
Accepted Answer
Star Strider
on 31 Dec 2015
Without knowing what the images are and seeing your code, it’s difficult to determine the problem. That they’re images means that they could have three dimensions (the third being the colour channels in a RGB image), and if one is a grayscale image having two dimensions, you could get that error even if they have the same number of rows and columns.
21 Comments
Neo
on 31 Dec 2015
Edited: Star Strider
on 31 Dec 2015
They're both grayscale images and when I upload both of them their workspace dimensions are the same.
Sorry I forgot to add the code:
[irow, icol] = size(I);
[irow2, icol2] = size(I2);
data = reshape(I',irow*icol,1);
data2 = reshape(I2',irow2*icol2,1);
[M,N] = size(data);
[M2,N2] = size(data2);
% subtract off the mean for each dimension
mn = mean(data,2);
mn2 = mean(data2,2);
data = double(data);
data2 = double(data2);
data = data - repmat(mn,1,N);
data2 = data2 - repmat(mn2,1,N);
%Here i am calculating the covariance matrix between the two images
covariance = (1 / (N-1)) * (data2) .* (data2.');
can't access the images right now but I can tell you all the data compositions of both the images which should be sufficient. (hopefully for this moment at least).
Cheers,
Neo Cornel
Image Analyst
on 31 Dec 2015
To learn how to format your code: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Also, I'm pretty sure I told you in your other question that you don't have to use repmat() to subtract a scalar from every element in the array.
So you're telling us that M=M2 and N=N2? Can you print them out to the command window and paste the results back here to prove it?
Star Strider
on 31 Dec 2015
covariance = (1 / (N-1)) * (data2) .* (data2.');
You have taken the simple transpose of ‘data2’ here, so the dimensions are not the same (unless the images are square matrices, that apparently they aren’t).
Neo
on 31 Dec 2015
Edited: Neo
on 31 Dec 2015
@ImageAnalyst: Thank you for the link for formating.
I am pretty sure you did too, but I you did not tell me why I did not have to do this. Is it because with grayscale images, 2D images with no RGB or miscellaneous, you don't have to do this, but calculate the slope of the best fit line? In other words, please explain why I don't have to do this.
Here is a pic of the command window.
@Stephen: I hope this picture also addresses your question.
@Star Strider: That does not make sense to me, you can see from the pciture that the variables are the same 512x512, so how can they not be square?
Star Strider
on 31 Dec 2015
You never before said they were square! How were we to know?
Besides, that isn’t the problem. Your ‘data’ and ‘data2’ matrices are (according to the picture) (262 x 144 x 1) double arrays.
Star Strider
on 31 Dec 2015
I excerpted a section of your picture.
Here it is:
Star Strider
on 31 Dec 2015
OK. I misread it initially. It’s not easy to read in the original .jpg. (I used .png here to avoid more distortion.)
If you want to calculate the covariance of the two vectors, why not just use the MATLAB built-in cov function?
Neo
on 31 Dec 2015
That worked, but that still doesn't explain why I had the error in the first place, I used covar = cov(data,data2'); and it gave me no error, but based on the formula for covariance (normalization constant)*(a)(b'), does cov take into account b' or as I have written is correct. i.e. is it covar = cov(data,data2); or covar = cov(data,data2');?
Image Analyst
on 31 Dec 2015
Neo, Star found the problem. Since both data and data2 are 262144 by 1 column vectors, and data2.' is a 1 by 262144 array. So the number of rows don't match (262144 vs. 1) AND the number of columns don't match (1 vs. 262144) so you cannot do an "element-by-element" multiplication.
Your formula for covariance does not even consider data - it only looks at data2. Why not just do
covariance = cov(data, data2)
like Star suggested?
Image Analyst
on 31 Dec 2015
If you do
m = randi(9, 2, 2)
offset = 2;
out = m - offset
% That is the same as
out = m - repmat(offset, [2, 2])
So why bother spending the time to make an array the same size of m just to subtract it when you don't have to?
Star Strider
on 31 Dec 2015
Edited: Star Strider
on 31 Dec 2015
You were attempting to do element-wise operations (the ‘.*’ instead of ‘*’) that requires the operands to have the same dimensions. (The exception to this convention is the transpose operator, where the dot (.') indicates the simple transpose. The default for (') is to take the complex-conjugate transpose. They're obviously the same for real variables.)
The result I believe you want is:
covariance = (1 / (M-1)) * [[(data.') * (data)] [(data.') * (data2)]; [(data.') * (data2)] [(data2.') * (data2)]];
to produce the same result as the cov function. (I used brackets around the elements to make the code a bit easier to read. They can be replaced by parentheses.)
That should work.
Note that ‘(normalization constant)*(a)(b')’ must assume row (not column) vectors, and implied dot-product vector multiplication.
Neo
on 31 Dec 2015
Edited: Neo
on 31 Dec 2015
Yes, forgot to mention thanks for the explanations. And cheers mate.
@Analyst, I did the repmat because I wanted to subtract the mean from each element in the area. To do this, would I not need to create the array?
Star Strider
on 1 Jan 2016
My pleasure! Cheers!
And to save Image Analyst some time, you do not have to use repmat to operate a scalar value with an array. The scalar is automatically expanded to the size of the array.
Image Analyst
on 1 Jan 2016
Neo, just try the code I posted in the comment above. You'll see you get the same result with and without doing repmat.
Image Analyst
on 4 Jan 2016
If you wanted to make copies of something. Like if you manually made up a 2-by-2 checkerboard and you wanted to replicate it to be a 8-by-8 checkerboard for example.
Star Strider
on 4 Jan 2016
... or for a number of other reasons, for instance if you are using textscan and want to replicate a format descriptor without typing out each one, use repmat('%f',1,10) to replicate ‘%f’ 10 times.
It’s highly useful, although if you’re operating on a matrix with a vector element-wise, bsxfun is usually faster.
More Answers (0)
See Also
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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)