Execution of for loop and indexing of strucutres

조회 수: 1 (최근 30일)
Waqas Syed
Waqas Syed 2015년 7월 9일
답변: Image Analyst 2015년 7월 10일
Hi,
I am trying to run the following command inside a for loop:
if A(p,1)== B(:,1)
My question: Is every row of B is being compared to A in one running of the loop?
Regards, Waqas
  댓글 수: 5
James Tursa
James Tursa 2015년 7월 10일
Can you describe, in words, what this test is supposed to do?
if MainWPR_mpc.branch(p:1)== Reus.bus(:,1)
The (p:1) indexing definitely does not look right. Nor does the (p:2) indexing in the following line.
Waqas Syed
Waqas Syed 2015년 7월 10일
Sorry, it was supposed to be a comma instead of a colon. Apologies for that.

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

채택된 답변

Image Analyst
Image Analyst 2015년 7월 10일
MainWPR_mpc.branch(p,1) == Reus.bus(:,1)
produces a boolean vector, a list of true false values, not just a single one. So if you do that, you have to decide exactly what you're testing. What if you get 3 true and 5 false, like [1,0,0,1,1,0,0,0]? What do you want to do then? If you want ALL to be true, use all(). If you want to check if ANY of them is true, use any:
if all(MainWPR_mpc.branch(p,1)== Reus.bus(:,1))
if any(MainWPR_mpc.branch(p,1)== Reus.bus(:,1))
I don't know which way you want it.

추가 답변 (1개)

James Tursa
James Tursa 2015년 7월 10일
편집: James Tursa 2015년 7월 10일
Is p the loop index?
A(p:1) could be empty or could be a scalar depending on what p is. B(:,1) is the first column of B. In an "if" test, the == would mean that all of the elemental results would need to be non-zero for the if-test to evaluate as true. I.e., all of the elements of the first B column would have to equal A(p:1) for the if-test to evaluate as true.
That being said, A(p:1) doesn't look right for what you probably intended and I would re-examine your code.
  댓글 수: 1
Waqas Syed
Waqas Syed 2015년 7월 10일
Sorry, it was supposed to be a comma instead of a colon. Apologies for that.

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

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by