if statement find criteria not doing what I want it to do
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Sara Ismail-Sutton
am 25 Nov. 2020
Beantwortet: madhan ravi
am 25 Nov. 2020
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
0 Kommentare
Akzeptierte Antwort
David Hill
am 25 Nov. 2020
function[out]=neqchek(k)
c=find(k==1)
if ~isempty(c)
k(1)=10
out=k
end
end
2 Kommentare
the cyclist
am 25 Nov. 2020
Bearbeitet: the cyclist
am 25 Nov. 2020
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.
Weitere Antworten (1)
madhan ravi
am 25 Nov. 2020
neqchek = @(k) subsasgn(k, substruct('()', {k(1) == 1}), 10);
out = neqchek([1 : 2, 1])
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!