Could anyone help me with the following code

code:
user=[2 4 ];
R=[2 3]
Xmax=1
Ymax=1
for t = 1:length(user)
for i = 1:length(R)
radius = R(i)
xsd=ones(1,user(t))*Xmax
ysd=ones(1,user(t))*Ymax
rrx=radius*sqrt(rand(1,user(t)))
thetarx=2*pi*rand(1,user(t))
xrx=xsd+rrx.*cos(thetarx)
yrx=ysd+rrx.*sin(thetarx)
th = 0:pi/100:2*pi
xunit = radius * cos(th) + Xmax
yunit = radius * sin(th) + Ymax
figure
plot(xunit, yunit)
hold on
plot(ysd,xsd,'r^')
hold on
plot(yrx,xrx,'ko')
hold on
end
end
I want to have the graph for 2 user with respect to R=[2 3] on the same figure such that the 2 users should be scatterd.Similarly for 4 user with respect to R=[2 3] on the same figure such that the 4 users should be scatterd.Could anyone please help me on this.

Answers (2)

Adam Danz
Adam Danz on 23 Jul 2018
Edited: Adam Danz on 23 Jul 2018
The template below will create a figure for each t-user and on each figure will be plotted all Rs for each t_th user.
for t = 1:length(user)
figure
axis
hold on
for i = 1:length(R)
plot()
end
end
This second template will create only 1 figure and all data will be plotted on the same axis.
figure
axis
hold on
for t = 1:length(user)
for i = 1:length(R)
plot()
end
end
Did that answer your question?

9 Comments

I used the first template in my code
user=[2 4]
Xmax=1
Ymax=1
R=[2 3]
for t = 1:length(user)
figure
axis
hold on
for i = 1:length(R)
radius = R(i)
xsd=ones(1,user(t))*Xmax
ysd=ones(1,user(t))*Ymax
rrx=radius*sqrt(rand(1,user(t)))
thetarx=2*pi*rand(1,user(t))
xrx=xsd+rrx.*cos(thetarx)
yrx=ysd+rrx.*sin(thetarx)
th = 0:pi/100:2*pi
xunit = radius * cos(th) + Xmax
yunit = radius * sin(th) + Ymax
figure(1)
plot(xunit, yunit)
hold on
plot(ysd,xsd,'r^')
hold on
plot(yrx,xrx,'ko')
hold on
end
end
But i am unable to get the graph as stated in the first template. Could you please help me on this.
Adam Danz
Adam Danz on 23 Jul 2018
Edited: Adam Danz on 23 Jul 2018
I need to know more specifically what the problem is with the new version of your code. Also, there's no need for you to call 'figure(1)' in your for-loop. Also, you can remove all the 'hold on' commands within the for-loop. Could you describe your goal?
the reason to call figure(1) is to have two different radius on the same graph.If i remove hold on i am unable to get the circle on the graph.
What I actually need is i need to have 2 graph one for user=2 with two different radius and another for user=4 with two different radius. Could you please help me on this.
Do you want two separate figures or do you want one figure with different axes on the figure?
I want two separate figures one with respect to user=2 containing two different radius and other with respect to user =4 with two different radius.
My template #1 does exactly what you're asking. As I mentioned, you can (and should) remove the 'hold on' lines within your for-loop since you're holding the axis as soon as it's created.
This code produces two figures (one for each user) Each figure has one axis. Each figure contains two circles.
user=[2 4 ];
R=[2 3]
Xmax=1
Ymax=1
for t = 1:length(user)
figure(); %CREATE A FIGURE FOR EACH USER
axh = axes(); %CREAT AXIS
hold(axh, 'on') %HOLD THE AXIS
for i = 1:length(R)
radius = R(i)
xsd=ones(1,user(t))*Xmax
ysd=ones(1,user(t))*Ymax
rrx=radius*sqrt(rand(1,user(t)))
thetarx=2*pi*rand(1,user(t))
xrx=xsd+rrx.*cos(thetarx)
yrx=ysd+rrx.*sin(thetarx)
th = 0:pi/100:2*pi
xunit = radius * cos(th) + Xmax
yunit = radius * sin(th) + Ymax
plot(axh, xunit, yunit)
plot(axh, ysd,xsd,'r^')
plot(axh, yrx,xrx,'ko')
axis equal %MAKE ASPECT RATIO EQUAL SO CIRCLES LOOK LIKE CIRCLES
end
end
Run this code. If it's not doing what you want it to do try again at describing what you'd like.
ok.I am getting two separate figures each with two different radius.But for user=2 i need to have in total of 2 users in one figure,but i am getting 4 instead of 2.In the same way in another figure i am getting 8 instead of 4.Could you please help me on this.
I don't know what you're code is supposed to do so I can only take guesses. I think the error might be in how you're using the ones() and rand() functions.
users = [2,4];
t = 2;
ones(1,user(t)); %== ones(1,4); you're creating 4 ones.
rand(1,user(t)); %== rand(1,4); you're creating 4 random numbers
Do you mean
rand(1,length(user)) %? This creates 2 numbers, one for each user.
Since you're creating 4 ones in xsd and ysd of course you'll get 8 data points at the end of that loop.
This is where learning how to use debug mode is critical in troubleshooting your code.
i want two separate figures with respect to 2 user and 4 user. If it is with respect to 2 user the figure should contain only 2 users with respect to two different radius.But according to the code i am getting 2 users each for different radius which i doesnt need.so in total with respect to two radius i need to get 2 users which i actually need.Could you please help me on this.
Prabha, I don't understand what you need. Please describe each figure or draw a picture and upload it.
Tell me what's wrong here:
  1. you want two figures
  2. In figure 1, there will be data from 2 users (4 data points).
  3. In figure 2, there will be data from 4 users (8 data points).
  4. In figure 1, there should be two circles.
  5. In figure 2, there should also be two circles.
More importantly, is this your code or are you trying to adapt someone else's code? Have you tried stepping through the code in debug mode to understand what the problem is? Please reply to those questions first before I can help you more.
I want two separate figures one with respect to user=2 containing two different radius and other with respect to user =4 with two different radius.

This question is closed.

Tags

Asked:

on 23 Jul 2018

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!