Info

This question is closed. Reopen it to edit or answer.

Error in plot code

3 views (last 30 days)
Ayesha
Ayesha on 15 Feb 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
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');

Answers (1)

Walter Roberson
Walter Roberson on 15 Feb 2014
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 Comments
Ayesha
Ayesha on 15 Feb 2014
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 on 15 Feb 2014
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?

This question is closed.

Tags

Community Treasure Hunt

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

Start Hunting!