이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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에서 Read, Write, and Modify Image에 대해 자세히 알아보기
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 (한국어)