Matrix dimension must agree to use .*

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

 Réponse acceptée

Star Strider
Star Strider le 31 Déc 2015

0 votes

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 commentaires

Neo
Neo le 31 Déc 2015
Modifié(e) : Star Strider le 31 Déc 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
Image Analyst le 31 Déc 2015
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?
Please tell us the output of these exact commands:
size(I)
size(I2)
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
Neo le 31 Déc 2015
Modifié(e) : Neo le 31 Déc 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
Star Strider le 31 Déc 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.
Neo
Neo le 31 Déc 2015
@Strider: Sorry, I thought I said this more clearer. And I see 262144x1, am I misunderstanding something here? I don't see 262x144x1.
Star Strider
Star Strider le 31 Déc 2015
I excerpted a section of your picture.
Here it is:
Neo
Neo le 31 Déc 2015
There is no X inbetween the 262 and 144 so why is it understood as 262x144x1?
Star Strider
Star Strider le 31 Déc 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
Neo le 31 Déc 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');?
Neo
Neo le 31 Déc 2015
@Analyst:
Also, instead of repmat would it be something along the lines of data = data - mn? What would I use to subtract the scalar instead?
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?
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
Star Strider le 31 Déc 2015
Modifié(e) : Star Strider le 31 Déc 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
Neo le 31 Déc 2015
Modifié(e) : Neo le 31 Déc 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
Star Strider le 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
Image Analyst le 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.
Neo
Neo le 4 Jan 2016
When would I have to use repmat when doing array operations?
Image Analyst
Image Analyst le 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
Star Strider le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by