フィルターのクリア

if statement find criteria not doing what I want it to do

1 回表示 (過去 30 日間)
Sara Ismail-Sutton
Sara Ismail-Sutton 2020 年 11 月 25 日
回答済み: madhan ravi 2020 年 11 月 25 日
if k does not contain 1 matlab returns ' 1×0 empty double row vector ' so i dont understand why this is not working in my if statement, where I have wrote ' 1×0 empty double row vector' as 'zeros(1,0)'
if k does contain 1 it returns scalar 1
function[out]=neqchek(k)
c=find(k==1)
if c~=zeros(1,0)
k(1)=10
out=k
end
end

採用された回答

David Hill
David Hill 2020 年 11 月 25 日
function[out]=neqchek(k)
c=find(k==1)
if ~isempty(c)
k(1)=10
out=k
end
end
  2 件のコメント
Sara Ismail-Sutton
Sara Ismail-Sutton 2020 年 11 月 25 日
okay many thanks, and would you be able to point our for me what is wrong with using zeros(0,1) ? :))
the cyclist
the cyclist 2020 年 11 月 25 日
編集済み: the cyclist 2020 年 11 月 25 日
The reason your code doesn't work is that checking "equality" between two empty arrays does not return a simple true value. This is why your if statement doesn't behave as you expect.
Try something as simple as
[] == []
to see what I mean.

サインインしてコメントする。

その他の回答 (1 件)

madhan ravi
madhan ravi 2020 年 11 月 25 日
neqchek = @(k) subsasgn(k, substruct('()', {k(1) == 1}), 10);
out = neqchek([1 : 2, 1])
out = 1×3
10 2 1

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by