Matrix dimension must agree to use .*
1 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Star Strider
2015-12-31
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 个评论
Neo
2015-12-31
编辑:Star Strider
2015-12-31
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
2015-12-31
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
2015-12-31
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
2015-12-31
编辑:Neo
2015-12-31
@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
2015-12-31
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
2015-12-31
@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
2015-12-31
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
2015-12-31
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
2015-12-31
@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?
Image Analyst
2015-12-31
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
2015-12-31
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
2015-12-31
编辑:Star Strider
2015-12-31
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.
Star Strider
2016-1-1
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
2016-1-1
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
2016-1-4
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
2016-1-4
... 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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)