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

Nur Najiha
Nur Najiha 2011년 7월 28일
owh.. how to write the code properly in here.. sorry .. i really dont know how to do that...

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

 채택된 답변

Walter Roberson
Walter Roberson 2011년 7월 28일

0 개 추천

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

Nur Najiha
Nur Najiha 2011년 7월 28일
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..
Walter Roberson
Walter Roberson 2011년 7월 28일
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

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

추가 답변 (2개)

the cyclist
the cyclist 2011년 7월 28일

0 개 추천

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

Nur Najiha
Nur Najiha 2011년 7월 28일
yup.. tq for the link.. i'll learn it slowly.. by the way, is there any suggestion from u? :)
the cyclist
the cyclist 2011년 7월 28일
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.

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

Nathaniel Ewing
Nathaniel Ewing 2012년 4월 29일

0 개 추천

@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.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by