command asking to repeat previous step

[EDIT: 20110728 14:37 CDT - reformat - WDR]
Hello.. my question is: How to write programming code asking to return to the previous step i.e if A=B; we hv to return to step 2 (do all over again starting from step 2). my code is
for s = 1:23
d{s,1} = b{s,1}*(b{s,1})';
A{s,1} = eig(d{s,1}); %Find the eigenvalues
anyNegativeA = any(A(:)<0); %thanks to 'the cyclist' for the code..
if anyNegativeA; %checking if there's any entries in -ve value.
B = A*2;
end
end
...and now, what's the command asking to repeat the 'find the eigenvalues' step

1 Comment

owh.. how to write the code properly in here.. sorry .. i really dont know how to do that...

Sign in to comment.

 Accepted Answer

If A=B then because B=A*2, it must be the case that A=A*2 . That can only be true if every entry in A is one of (0, infinity, -infinity, or NaN). Note though that if that is the case, the only one of those that is negative for the anyNegative test is -infinity, so the B=A*2 test would only be invoked if at least one of the eigenvalues comes out as -infinity.
After you change B, that does not feed back to changing anything that is used do calculate the eigenvalues. The eigenvalues are determined entirely based upon the cell array "b" indexed at "s", but you have not changed "b" or "s". Going back and recalculating the eigenvalues would give you the same result, so you would infinite loop.
I think you need to reconsider your logic.

2 Comments

emmm.. actually B=A*2 is just dummy.. the real is B = A.*phi-diag(diag(A)).. just as an example.. i just wanna know how to write the command asking to repeat previous step.. tq for ur cooperation..
In this circumstance, the command asking to repeat the previous step can be optimized to the following code:
%
That is, the empty comment.
Repeating the previous step is not going to produce a different outcome than before unless you change "b" or "s".
For whatever good it does you:
while true
%do your eig computation here
if condition_to_exit_is_true; break; end
%change whatever variables needed to make the next eig round different
end

Sign in to comment.

More Answers (2)

the cyclist
the cyclist on 28 Jul 2011
Nur, the things you are asking are very basic MATLAB questions, or really even just very basic programming questions. This is not really a very good place to learn basic programming. I suggest, if you want to learn the basics of MATLAB, to thoroughly digest this:

2 Comments

yup.. tq for the link.. i'll learn it slowly.. by the way, is there any suggestion from u? :)
My guess is that you actually will want to use a "while" loop structure, which typically better for situations where you want to check whether a certain condition is true, then run the loop again depending on the result.

Sign in to comment.

@the cyclist
Unfortunately this is what's happening in engineering schools right now. I'm currently a mechanical engineering student, and what they've done to the curriculum is basically throw you into an engineering lab without knowing any matlab or even basic programming. It's a big shock and it's hard to learn Matlab while trying to get a good grade in a lab. Fortunately they're starting to restructure so next year's freshman will be taking all beginning programming their freshman year as well.

Categories

Community Treasure Hunt

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

Start Hunting!