how can I plot an array by array?

조회 수: 5 (최근 30일)
Moj
Moj 2014년 12월 12일
답변: Image Analyst 2014년 12월 12일
Dear community,
I want to plot an array by array but I don't know how to do this. x and y are 1 * 300 arrays.
for i = 7e-5:1e-5:3e-4
x(1,:) = 1 + 2*i;
y(1,:) = 3 + i;
end
plot(x,y)
how can I plot this easy problem?
  댓글 수: 1
Guillaume
Guillaume 2014년 12월 12일
Can you explain better what you're trying to do?
Note that if x and y are 1*300 arrays, your loop is equivalent to:
i = 7e-5:1e-5:3e-4 %24 elements
x(1:24) = 1 + 2*i
y(1:24) = 3 + i
That is, you're replacing the first 24 elements of x and y.

댓글을 달려면 로그인하십시오.

채택된 답변

Thorsten
Thorsten 2014년 12월 12일
val = 7e-5:1e-5:3e-4;
for i=1:length(val), x(i) = 1 + 2*val(i); y(i) = 3 + val(i);end
plot(x,y)
  댓글 수: 1
Guillaume
Guillaume 2014년 12월 12일
So Moj, if that's what you were looking for, what does the x and y are 1 * 300 arrays has to do with it.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 12월 12일
Assuming you want to do 300 values, I think the most MATLAB-ish way is this vectorized way:
% Define values
values = linspace(7e-5, 3e-4, 300) % 300 elements
% Create x and y vectors from those values.
x = 1 + 2 * values
y = 3 + values
% Plot y vs. x.
plot(x, y, 'b-', 'LineWidth', 2);
% Make the plot fancy.
grid on;
fontSize = 30;
title('y vs. x', 'FontSize', fontSize);
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

Community Treasure Hunt

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

Start Hunting!

Translated by