How to compare an integer to the elements of a vector?
19 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
azarang asadi
am 16 Sep. 2020
Beantwortet: Star Strider
am 16 Sep. 2020
So I have a single value u ( between 0 and one generated by rand) and I have a n dimensional vector, let's say C .I want to compare this u to every element of the C vector and find the lowest i such that u < C(i), for example u = 0.2 and C = [0.1 0.15 0.21 0.37 0.54 0.87 1].' , then my algorithms needs to find 0.21 which is C(3) and gives me the i (=3 in this case) back.
0 Kommentare
Akzeptierte Antwort
Star Strider
am 16 Sep. 2020
Another approach:
val = min(C(u<C));
idx = find(C == val);
.
0 Kommentare
Weitere Antworten (1)
KSSV
am 16 Sep. 2020
idx = find(u<C) ;
idx(1)
Read about find, you should have an option of getting first index striaght away.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Calendar finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!