How can the nested if statement in the last for loop be rewritten to be shorter?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
clear; close all
n = 4000;
% (A) Use rand to generate randomly distributed points:
Rand_Number = rand(n, 2);
x = Rand_Number(:,1)*2-1;
y = Rand_Number(:,2)*2-1;
hold on
figure(1)
subplot(3,1,1)
plot(x,y,'r.')
title('Random Points Generated Using "rand"')
xlabel('x')
ylabel('y')
subplot(3,1,2)
count = zeros(1,20);
for i=1:length(x)
count = histcounts(x, -1:0.1:1);
end
X=-.95:.1:1;
bar(X,count)
title('Distribution of Points')
xlabel('x')
ylabel('y')
subplot(3,1,3)
hist(x)
title('Histogram of Points')
xlabel('x')
ylabel('y')
hold off
delta_x = 0.1;
for j=1:200
tmp = rand(n,1);
for i = 1:length(x)
if tmp < 0.5
x(i)=x(i)+delta_x;
else
x(i)=x(i)-delta_x;
end
end
end
xmove=x
countmove = zeros(1,20);
for i=1:length(x)
if xmove(i)>= -5.0 && xmove(i)<=-4.5
count(1) = count(1)+1;
elseif xmove(i)>-4.5 && xmove(i)<-4.0
count(2) = count(2)+1;
elseif xmove(i)>-4.0 && xmove(i)<-3.5
count(3) = count(3)+1;
elseif xmove(i)>-3.5 && xmove(i)<-3.0
count(4) = count(4)+1;
elseif xmove(i)>-3.0 && xmove(i)<-2.5
count(5) = count(5)+1;
elseif xmove(i)>-2.5 && xmove(i)<-2.0
count(6) = count(6)+1;
elseif xmove(i)>-2.0 && xmove(i)<-1.5
count(7) = count(7)+1;
elseif xmove(i)>-1.5 && xmove(i)<-1.0
count(8) = count(8)+1;
elseif xmove(i)>-1.0 && xmove(i)<-0.5
count(9) = count(9)+1;
elseif xmove(i)>-0.5 && xmove(i)<0.0
count(10) = count(10)+1;
elseif xmove(i)>0.0 && xmove(i)<0.5
count(11) = count(11)+1;
elseif xmove(i)>0.5 && xmove(i)<1.0
count(12) = count(12)+1;
elseif xmove(i)>1.0 && xmove(i)<1.5
count(13) = count(13)+1;
elseif xmove(i)>1.5 && xmove(i)<2.0
count(14) = count(14)+1;
elseif xmove(i)>2.0 && xmove(i)<2.5
count(15) = count(15)+1;
elseif xmove(i)>2.5 && xmove(i)<3.0
count(16) = count(16)+1;
elseif xmove(i)>3.0 && xmove(i)<3.5
count(17) = count(17)+1;
elseif xmove(i)>3.5 && xmove(i)<4.0
count(18) = count(18)+1;
elseif xmove(i)>4.0 && xmove(i)<4.5
count(19) = count(19)+1;
elseif xmove(i)>4.5 && xmove(i)<=5.0
count(20) = count(20)+1;
end
end
figure(2)
subplot(2,1,1)
plot(xmove,y,'r.')
title('Moved Random Points Generated')
xlabel('x')
ylabel('Number of Points')
subplot(2,1,2)
Y = -4.75:0.5:4.75;
bar(Y,countmove)
title('Distribution of Points')
xlabel('x')
ylabel('Number of Points')
6 comentarios
Adam
el 4 de Feb. de 2017
Editada: Adam
el 4 de Feb. de 2017
Maybe they should put their question on Cody if they want 3 or more different ways to solve the same problem!
An answer being accepted suggests it fits the problem, otherwise it shouldn't be accepted. Most of us are more concerned with having wasted time if we answer a question that is also being asked in numerous different places to. Whether the answer was accepted or not doesn't really make any difference, but someone asking the same question 3 times having already accepted 2 answers is clearly just wasting people's time even if in this case the answers were largely trivial.
Walter Roberson
el 4 de Feb. de 2017
Please confirm that you do not want to count any value which is exactly -4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, or 4.5, but that you do want to count values which are exactly -4.5 or 5.0 ?
Respuestas (1)
Jan
el 2 de Feb. de 2017
Seriously? The answer given in http://www.mathworks.com/matlabcentral/answers/323076-how-do-you-rewrite-the-nested-if-statement-as-a-loop can be copied and adjusted easily. Again you do not need a loop, but one single command:
count = histcounts(x, -5:0.5:5)
Are you looking for something else?
0 comentarios
Ver también
Categorías
Más información sobre Title en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!