Matlab function calling basic -
i'm new matlab , learning basic grammar.
i've written file getbin.m:
function res = getbin(num_bin, bin, val) if val >= bin(num_bin - 1) res = num_bin; else = (num_bin - 1) : 1 if val < bin(i) res = i; end end end and call with:
num_bin = 5; bin = [48.4,96.8,145.2,193.6]; % bin stands intermediate borders, there 5 bins fea_val = getbin(num_bin,bin,fea(1,1)) % fea pre-defined 280x4096 matrix it returns error:
error in getbin (line 2) if val >= bin(num_bin - 1) output argument "res" (and maybe others) not assigned during call "/users/mac/documents/matlab/getbin.m>getbin". could tell me what's wrong here? thanks.
you need ensure every possible path through code assigns value res.
in case, looks that's not case, because have loop:
for = (num_bins-1) : 1 ... end that loop never iterate (so never assign value res). need explicitly specify it's decrementing loop:
for = (num_bins-1) : -1 : 1 ... end for more info, see documentation on colon operator.
Comments
Post a Comment