"Matrix dimensions must agree"

조회 수: 1 (최근 30일)
Evans Gyan
Evans Gyan 2018년 1월 27일
댓글: Evans Gyan 2018년 1월 30일
Am trying to do some calculation between two matrix. unfortunately i keep getting this error. Other methods that i have tried such as bsxfun(@times run my computer into space. Any help given will be much appreciated. Here is the line of code
val = load ('edata.txt');
for data=2:1:length(val)-1
if(val(data) > val(data-1) & val(data) > val(data+1) & val(data) > 4)
val1(data) = 1;
else
val1(data) = 0;
end
end
figure
plot((val-min(val))/(max(val)-min(val)), '-g'); title('\bf Prominent Detection Peaks');
hold on
stem(val1'.*((val-min(val))/(max(val)-min(val)))', ':k');
hold off
The of dimension of val1 = 1x7167 and val contain 7168x1 Thanks in advance
  댓글 수: 6
Guillaume
Guillaume 2018년 1월 28일
편집: Guillaume 2018년 1월 28일
"The intent is [repeat of code that is in the question]"
There's absolutely no point in restating (for the 2nd time) the code that is in the question. We understand the code just fine. It can't work since the matrix dimensions don't agree.
We'd be a lot further if you answered the question we asked. So I repeat:
Is the intent to obtain a 7167x7168 matrix (which is what you'd use bsxfun for) or a vector of some length, possibly 7167 or 7168 (for which you'd never use bsxfun)?
If it's a vector, what do you expect element 7168 of val to get multiplied with?
Evans Gyan
Evans Gyan 2018년 1월 28일
The intent is to obtain a vector of length. Element 7168 should multiply with the last element of val (if its possible). The length of val1 fell short of 1 and i dont know why it occurred after going over the loop.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2018년 1월 28일
Because val1 is not preallocated in the code you've showned, it isn't obvious that is created by the loop.
val1 is shorter by 1 element than val because you explicitly construct it so. Your loop is
for loopindex 2 : numel(val)-1 %data as a name for a loop index is extremely misleading
val1(loopindex) = ...
end %last index is numel(val)-1. Hence val1 has length numel(val)-1
If you want val1 to be the same length as val then you can assign 0 to the last element, the same way 0 is automatically assigned to val1(0) at the first step of the loop when it does val(2) = .... So after the loop you could have:
val1(end + 1) = 0;
But the loop is completely unneeded and is a very slow way to construct val1 (particularly without the aforementioned preallocation). This would achieve the same, including creating val1 the correct length:
valdiff = diff(val);
val1 = [false, valdiff(1:end-1) > 0 & valdiff(2:end) < 0 & val(2:end-1) > 4, false];
With that fix your stem line will work.
No idea why you tried to use bsxfun or why you mentioned it. There is nowhere in your code where it could be used.
  댓글 수: 3
Guillaume
Guillaume 2018년 1월 29일
I assumed val was a row vector. If val is a column vector then replace the , by ; for vertical concatenation:
val1 = [false; valdiff(1:end-1) > 0 & valdiff(2:end) < 0 & val(2:end-1) > 4; false];
Evans Gyan
Evans Gyan 2018년 1월 30일
It worked Guillaume. Thank you so much. You saved me the hassle. Perfect touch

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by