why does my figure doesn't show

I have data points 1000 and for the first 1000 I'm using mean [-2 1] and std of 0.85 and for the other 1000 I'm using mean [4 5] and std 2 and I'm trying to plot the data but I'm getting this error
function kmean( )
data1 = 1000;
s =[-4 0;1 0.75];
a =[3 0;4 2.0];
data3 = s * data1;
data4 = a * data1;
figure
plot(data3(:,1) , data3(:,2) , 'r.');
plot(data4(:,1) , data4(:,2) , 'g.');
xlabel('x-value');
ylabel('y-value');
axis square
axis ([-10 10 -10 10])
end

 Accepted Answer

My guess is that the ‘data3’ plot doesn’t show.
There are two ways to correct that:
1. Use the hold function:
figure
plot(data3(:,1) , data3(:,2) , 'r.');
hold on
plot(data4(:,1) , data4(:,2) , 'g.');
hold off
xlabel('x-value');
ylabel('y-value');
axis square
axis ([-10 10 -10 10])
2. Plot two separate figures:
figure(1)
plot(data3(:,1) , data3(:,2) , 'r.');
xlabel('x-value');
ylabel('y-value');
axis square
axis ([-10 10 -10 10])
figure(2)
plot(data4(:,1) , data4(:,2) , 'g.');
xlabel('x-value');
ylabel('y-value');
axis square
axis ([-10 10 -10 10])
MATLAB will plot subsequent plot calls in the existing axes, overwriting the existing plot, unless you use the hold function.

6 Comments

I missed something on my previous I forget to multiply by randn here is my changing code
data1 = 1000;
s =[-2 0;1 0.85];
a =[4 0;5 2];
data3 = s * randn(data1,1);
data4 = a * randn(data1,2);
figure
plot(data3(:,1) , data3(:,2) , 'r.');
plot(data4(:,1) , data4(:,2) , 'g.');
xlabel('x-value');
ylabel('y-value');
axis square
axis ([-10 10 -10 10])
however I get these two errors Error using * Inner matrix dimensions must agree.
Error in kmean (line 27) data4 = a * randn(data1,2);
>> kmean Error using * Inner matrix dimensions must agree.
Error in kmean (line 26) data3 = s * randn(data1,1);
I don’t know what you’re doing. You cannot calculate ‘data3’ and ‘data4’ that way (multiplying a (2x2) matrix by a (1000x1) vector).
Your plot statement still won’t work the way you’ve written it if you want to see both plots.
I think my calculation is wrong can you please give me simple example on how to generate 2d cluster of samples 1000 and mean [4 5] and std 2
I am not certain what you mean by ‘2d cluster of samples’. You can create a normally-distributed random (1000x2) matrix with those characteristics easily enough:
N = [4+2*randn(1000,1) 5+2*randn(1000,1)];
Check = [mean(N); std(N)] % Check Results
Here, ‘N’ is your (1000x2) matrix, and the ‘Check’ assignment just shows that it creates the vectors you want. Delete ‘Check’ afterwards, since it’s not necessary for the code.
thank you very much this is what I was looking for your answers are always detailed and helped me a lot
As always, my pleasure!

Sign in to comment.

More Answers (0)

Asked:

on 21 Jan 2016

Commented:

on 21 Jan 2016

Community Treasure Hunt

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

Start Hunting!