Using 'for loop' to plot
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have three set of data (y1, y2 and y3). And I want to only plot a certain range within each data set.
I want to perform this task in two ways. I succedded in the first method to plot the graph without a 'for loop'.
But in the second method, I want to use a 'for loop'. However, I am a bit confused. Can you help me out. Thanks
clear all; close all;clc
x1=linspace(0, 11, 25);y1=2*x1.^2;
f1=max(find(x1<=2));f2=min(find(x1>=10));
x2=linspace(-2, 15, 17);y2=x2.^2;
f3=max(find(x2<=3));f4=min(find(x2>=14));
x3=linspace(-5, 25, 21);y3= (7.*x3)+ 5;
f5=max(find(x3<=5));f6=min(find(x3>=21));
%% Method 1
plot(x1(f1:f2),y1(f1:f2), 'r');hold on;plot(x2(f3:f4),y2(f3:f4), 'k');hold on;plot(x3(f5:f6),y3(f5:f6),'b')
xlabel('X');ylabel('Y')
return
%% Method 2
A=[y1 y2 y3];
B=[x1 x2 x3];
figure;
for i=1:length(A);
for j=1:length(B);
f1=max(find(x1<=2));f2=min(find(x1>=10));
f3=max(find(x2<=3));f4=min(find(x2>=14));
f5=max(find(x3<=5));f6=min(find(x3>=21));
plot(B(i),A(i))
hold on;
end
end
2 Kommentare
Walter Roberson
am 2 Dez. 2022
Note that you can do things like
mask = x1 > 2 & x1 <10;
plot(x1(mask), y1(mask))
Akzeptierte Antwort
Karim
am 3 Dez. 2022
It's not really clear why you would like to use a loop since the vectorized method is a lot cleaner from a code perspective. However, below you can see one method to use a for loop with the concatinated vectors A and B.
x1 = linspace(0, 11, 25);
y1 = 2*x1.^2;
i1 = x1 >=2 & x1 <= 10; % set up index for plot 1
x2 = linspace(-2, 15, 17);
y2 = x2.^2;
i2 = x2 >=3 & x2 <= 14;
x3 = linspace(-5, 25, 21);
y3 = (7.*x3)+ 5;
i3 = x3 >=5 & x3 <= 21;
%% Method 1 - vectorized
figure
hold on
plot(x1(i1), y1(i1), 'r')
plot(x2(i2), y2(i2), 'k')
plot(x3(i3), y3(i3), 'b')
hold off
xlabel('X')
ylabel('Y')
title("Method 1 - vectorized")
grid on
%% Method 2 - using a loop
A = [y1 y2 y3];
B = [x1 x2 x3];
figure
hold on
for i = 1:length(A)
if i <= length(x1)
% we are using the values for x1 y1
minVal = 2;
maxVal = 10;
colVal = 'r';
elseif i <= (length(x1)+length(x2))
% we are using the values for x2 y2
minVal = 3;
maxVal = 14;
colVal = 'k';
else
% we are using the values for x3 y3
minVal = 5;
maxVal = 21;
colVal = 'b';
end
if B(i) >= minVal & B(i) <= maxVal
scatter(B(i),A(i),colVal,'filled')
end
end
hold off
xlabel('X')
ylabel('Y')
title("Method 2 - in a loop")
grid on
0 Kommentare
Weitere Antworten (1)
Walter Roberson
am 3 Dez. 2022
Of course you can do it with a loop.
I am not clear as to why you are constructing an x range that is not just the inside of the range that you are masking off, but whatever...
xlow = [0, -2, -5];
xhigh = [11, 15, 25];
masklow = [2, 3, 5];
maskhigh = [10, 14, 21];
colors = {'r', 'k', 'b'};
polycoeffs = [2 0 0; 1 0 0; 0 7 5];
for K = 1 : length(xlow)
x = linspace(xlow(K), xhigh(K));
y = polyval(polycoeffs(K,:), x);
mask = masklow(K) < x & x < maskhigh(K);
plot(x(mask), y(mask), colors{K});
hold on
end
hold off
xlabel('X');
ylabel('Y')
Siehe auch
Kategorien
Mehr zu Line Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!