"Matrix dimensions must agree"
3 次查看(过去 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
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?
采纳的回答
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
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];
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!