MATLAB: What is wrong with the code short simple function using only – if,for,length and variables)

for loopfunctionifMATLABvariable

I needed to write a function that uses only if for length and variables and calls another function – valFind(that runs very well-added below), my function gets:1) a vector 2)an arrays of numbers to search for and brings back the first places in which those numbers appear in the vector (or 0 if they don't appear) for example:
% code

a=[2 4 6 8 4 2]
b=[2 8 9]
c=valFindAll(a,b) --> [1 4 0]
this is valFind (runs fine the problem is in the text function)
% code
function [result] = valFind(a,b)
result = [];
for ii=1:length(a)
if a(ii) == b
result = [result ii];
this is the function that (I don't know why) doesn't run
% code
function [resultVec] = valFindAll(a,v)
resultVec =[];
for ii=1:length(v)
if result == []
resultVec = [resultVec 0];
resultVec = [resultVec result(1)];

Best Answer

  • Just change
    result == []
    length(result) == 0
    and everything will work fine.