Info

この質問は閉じられています。 編集または回答するには再度開いてください。

What's wrong with the code?

1 ビュー (過去 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}(:) = [];
end
end

回答 (1 件)

Adam
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 =
1410
2100
2850
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 ) )
or
if ( any( P{k}(:) == 0 ) )
  2 件のコメント
Adam
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