how to make an indicator function in loop
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jun Yoo
am 2 Sep. 2020
Bearbeitet: Jun Yoo
am 2 Sep. 2020
Dear Community,
Hi, im learning Matlab and wondering how to create an indicator function that I can implement in for loop.
for example, y is 10 X 1 array that has "1","2","3" as its values. What I want to do in R version is
for i = 1: length(x)
ll[i] = ifelse(y[i] == 1, 1, 0) * log(p0) + ifelse(y[i]==2,1,0)*log(p1) + ifelse(y[i]==3,1,0)*log(p2)
like if y(i,:) is 1 then 1 else 0 and multiply log(p0) and if y(i,:) is 2 then 1 else 0 and multiply log(p1)
I wanna create another ll and save the caculated values from the loop. is there any nice way to substitute "ifelse" in the calculation?
0 Kommentare
Akzeptierte Antwort
Alan Stevens
am 2 Sep. 2020
Bearbeitet: Alan Stevens
am 2 Sep. 2020
You could do the following in Matlab:
for i = 1:length(x)
Y = y(i);
ll(i) = log(p0)*(Y==1) + log(p1)*(Y==2) + log(p2)*(Y==3);
end
Note that log is log to base e. log10 is log to base 10.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Entering Commands 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!