Info

此问题已关闭。 请重新打开它进行编辑或回答。

Error in plot code

3 次查看(过去 30 天)
Ayesha
Ayesha 2014-2-15
关闭: MATLAB Answer Bot 2021-8-20
The code is very simple where I have to plot the denoising error as a function of mu. how ever I am getting error at
err(i) = abs(a1);
with the message : In an assignment A(I) = B, the number of elements in B and I must be the same. I see absolutely nothing that could logically go wrong but still, what's wrong?
mulist = linspace(.1,3.5,31);
err = zeros(1,length(mulist));
for i = 1:length(mulist)
a1=x-x0;
err(i) = abs(a1);
end
figure;
plot(mulist,err, '.-'); axis('tight');
set_label('\mu', 'SNR');

回答(1 个)

Walter Roberson
Walter Roberson 2014-2-15
You have not show us x or x0. One or both of them are vectors (or arrays), and you are trying to store abs() of the vector arithmetic inside the single element err(i) .
Notice that your "for i" loop goes to length of mulist but "a1=x-x0" does not depend upon mulist at all, so each a1 value would be exactly the same.
  2 个评论
Ayesha
Ayesha 2014-2-15
Here's the complete set. I understand what you're saying and I've for the problem but all in vain. It's kind of confusing to me because the code is running fine when I'm using snr operation instead of minus whereas its not otherwise.
name = 'piece-regular';
n = 1024;
x0 = load_signal(name,n);
x0 = rescale(x0);
sigma = .04;
x = x0 + sigma*randn(size(x0));
mulist = linspace(.1,3.5,31);
err = zeros(1,length(mulist));
for i = 1:length(mulist)
a1=x-x0;
err(i) = abs(a1);
end
Walter Roberson
Walter Roberson 2014-2-15
snr() takes in a vector and returns a single value, the signal to noise ratio.
minus of two vectors returns another vector the length of the originals.
[9, 5, 2] - [4, 11, 21] = [5, -6, -19]
Are you trying to find the "distance" between the two vectors? If so then minus is not the right operation for that. For that you would want something like the Euclidean Distance,
sqrt(sum((x-x0).^2))
Perhaps, though, you are trying to find the correlation of the two vectors. Or perhaps you are trying to find the lag between them. Or perhaps you are trying to find a measure of whether they are considered to be drawn from the same distribution. Or... lots of things you might have intended. What were you thinking that minus would do for you?

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by