# An elegant alternative to nested for loops for plotting simulations

1 view (last 30 days)
Sarah on 13 Jul 2013
Hi everyone,
I am plotting a 3D simulation plot using the plot3 function. Here is the code:
clear all; clc; close all;
%For loop is used to simulate iteration over time. So I is each time
%instant. I am plotting one data point for each time instant. It might make
%more sense if MarkerSize = 'b.';
MarkerSize = 'b-';
for I =1:91
for J = 1:100
plot3(Location(J,1:I),Time(J,1:I),Magn(J,1:I),MarkerSize)
hold on
end
xlim([0 100]);
ylim([0 100]);
zlim([0 1.5]);
xlabel('Location');
ylabel('Time');
zlabel('Magnitude');
grid on;
pause(1);
end
Here:
Location = 100x91 Matrix
Time = 100x91 Matrix
Mag = 100x91 matrix
Essentially, there are 100 locations. Each of these 100 locations has a time history of 91 data points, where each data point corresponds to some magnitude value over time. I want to simulate the data for all 100 locations changing over time. For example, for each iteration, there is a new data point across time.
My problem is this takes a long time to compute. Is there a more elegant way of doing this? Thanks for your time.

Pourya Alinezhad on 14 Jul 2013
Edited: Pourya Alinezhad on 14 Jul 2013
hi. do not use HOLD ON command. you can compute and save your data in another matrix.then plot the whole data generated at once.so you will run Plot command just once.here is an example :
plotting with hold on :
x=[0:0.2:20];
for i=1:3
y=sin(x/i)./sqrt(x+1);
plot(y)
hold on
end
plotting without using hold on :
x=[0:0.2:20];
y=sin(x)./sqrt(x+1);
y(2,: )=sin(x/2)./sqrt(x+1);
y(3, : )= sin(x/3)./sqrt(x+1);
plot(x,y);
or simpler:
x=[0:0.2:20];
for i=1:3
y(i,:)=sin(x/i)./sqrt(x+1);
end
plot(x,y)
just use the above examples with a little change for 3d case.