이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

What's wrong with the code?

조회 수: 3 (최근 30일)
George Ansari
George Ansari 2017년 8월 8일
마감: MATLAB Answer Bot 2021년 8월 20일
The code supposed to delete an element from B cell if corresponding P = 0. Please help me to fix it. Thanks, George.
L = [1 10 15];
L_cell = num2cell(L);
P_max = [100;150;200];
Pg = [90 150 150];
B = [1;2;3];
for k = 1:length(L)
P_d{k} = P_max*L_cell{k};
P{k}=P_d{k} - Pg';
B_cell{k} = num2cell(B);
if (P{k}(:) == 0)
B_cell{k}(:) = [];

답변 (1개)

Adam 2017년 8월 8일
편집: Adam 2017년 8월 8일
Simple use of the debugger or even just the command line would help you solve this.
>> P{k}(:)
ans =
This cannot be tested by equality against 0 in an if statement (or rather not in the way you would want it to be).
It isn't obvious what you want in this case, but
doc all
doc any
will solve the problem in 2 different ways depending on what logic you want, e.g.
if ( all( P{k}(:) == 0 ) )
if ( any( P{k}(:) == 0 ) )
  댓글 수: 2
George Ansari
George Ansari 2017년 8월 8일
Thank you Adam, I read documentation on 'all' and 'any', but don't think that any of these fit my needs.
What I'm trying to implement is the following. Assume there are 3 power generating units, each has its values of P_max, Pg and B. As it can be seen from the code 'P' is also individual to each unit. I calculate it for different 'L'(which is general to all units). When generator's P = 0, there is no power output from the generator, and therefore the corresponding B value must be deleted from the B vector (in other words B vector is reduced to 2 elements). I hope I was clear. George.
Adam 2017년 8월 8일
The point is though that P{k} contains 3 elements, not just one. So P == 0 does not make sense in this context.


Community Treasure Hunt

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

Start Hunting!

Translated by