Help with looping problem

Here I am defining x and y coordinates of rays vertically approaching a boundary (currently sin(x))
x = 0:0.1:pi
rx1 = [x(1) x(1)] % x-coordinates, incoming ray 1
ry1 = [y0 sin(x(1))] % y-coordinates, incoming ray 1
rx2 = [x(2) x(2)] % x-coordinates, incoming ray 2
ry2 = [y0 sin(x(2))] % y-coordinates, incoming ray 2
rx3 = [x(3) x(3)] % x-coordinates, incoming ray 3
ry3 = [y0 sin(x(3))] % y-coordinates, incoming ray 3
and so on for the length of vector (x) = 32
Clearly this isn't a good way to write the program. The alternative approach I have tried to use is the following:
i = 1
for i = 1:length(x)
rx(i) = [x(i) x(i)]
ry(i) = [y0 sin(x(i))]
end
Which returns the error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
So clearly this isn't the way to achieve what I was trying to do. I would greatly appreciate if someone could help me resolve this, as I have been stuck on it for some time.

 채택된 답변

Matt Fig
Matt Fig 2012년 10월 23일

0 개 추천

Why would you want to create 64 variables like that? What a mess it would be to deal with in further steps. Perhaps two cell arrays could get the job done:
for ii = 1:length(x)
rx{ii} = [x(ii) x(ii)]; % rx is a cell array.
ry{ii} = [y0 sin(x(ii))];
end
Now the really important question is why do you need to do this at all? What is your next step that requires you to have these arrays as individuals, rather than just using x and sin(x)?

댓글 수: 6

Tom
Tom 2012년 10월 23일
편집: Tom 2012년 10월 23일
Haha! I wouldn't be surprised to hear I have gone the complete wrong way about this. Perhaps I could ask you then: How would you plot a number of vertical lines in increments along the x-axis, which each terminate when they reach the function sin(x)?
Really appreciate the help by the way.
p.s. when I ran your suggestion I received the error: Error using plot Not enough input arguments.
Do this instead:
x = 0:0.1:pi;
y = sin(x);
plot(x,y,'k-')
hold on
for ii = 1:length(x)
line([x(ii) x(ii)],[-1 y(ii)])
end
Tom
Tom 2012년 10월 23일
That is exactly what I needed. I cannot thank you enough!
Matt Fig
Matt Fig 2012년 10월 23일
편집: Matt Fig 2012년 10월 23일
Or without the FOR loop at all:
x = 0:0.1:pi;
y = sin(x);
plot(x,y,'k-')
L = line([x;x],[-ones(1,length(x));y]);
set(L,'color','b')
Tom
Tom 2012년 10월 23일
I haven't encountered this 'line' parameter yet. How come it is plotted without even being asked?
LINE is not a parameter, it is a function.
help line

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

추가 답변 (3개)

Matt Kindig
Matt Kindig 2012년 10월 23일
편집: Matt Kindig 2012년 10월 23일

0 개 추천

I'm not entirely clear what you want, but I think this will do the trick:
x = (0:0.1:pi)';
rx = [x, x];
ry = [ repmat(y0, size(x)), sin(x)];
This implements the vector as specified in the for loop you have shown, which is the preferred way to specify matrix data in Matlab (not creating variables for each line, as your rx1, rx2, etc. example shows).

댓글 수: 1

Tom
Tom 2012년 10월 23일
편집: Walter Roberson 2012년 10월 23일
Thanks for the swift response!
That didn't seem to work, perhaps it would make my problem clearer if you run the following:
x = 0:0.1:pi;
y = sin(x);
plot(x,y,'k-')
hold on
rx1 = [x(1) x(1)]
ry1 = [-1 sin(x(1))]
rx2 = [x(2) x(2)]
ry2 = [-1 sin(x(2))]
rx3 = [x(3) x(3)]
ry3 = [-1 sin(x(3))]
plot(rx1,ry1,rx2,ry2,rx3,ry3)
This is what I am trying to achieve (although I have only shown the first 3 vertical lines).
My inexperienced attempt to achieve this with a For loop goes:
for i = 1:length(x)
rx(i) = [x(i) x(i)]
ry(i) = [y0 sin(x(i))]
end
Which returns the error shown above, making clear I'm going completely wrong. Any idea what I should be doing?

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

Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 23일

0 개 추천

y0=1
x = 0:0.1:pi
n=length(x);
eval(sprintf('rx%d=[x(%d) x(%d)],',repmat((1:n),3,1)))
eval(sprintf('ry%d=[y0 sin(x(%d))],',repmat((1:n),2,1)))

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

Tom
2012년 10월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by