Create an array based on another array's input
Mostrar comentarios más antiguos
Hello, I had this question in Class last weekend, but I seemed not capable of solving it ,despite it being pretty easy relatively (I had to use vectorized code) it wamted me to convert an array of scores into grades using vectorized code. My regular sol. Would be:
X=input('insert array');
j=1;
For i=1:length(x)
If x(i) >=90
F(j) == 'a';
j=j+1;
Elseif x(i) >= 80 & x(i)<= 90
F(j)== 'b';
End
End
But as you probably know this is not vectorized. If possible I would want to know multiple ways of solving that, so I can expand my understanding of matlab and vectors in general. Thanks alot and excuse my english.
Respuestas (2)
Roger Stafford
el 11 de Mayo de 2016
0 votos
Use the ‘histc’ function, [~,ix] = histc(x,grade_ranges), then use ix to index into a vector [‘A’,’B’,’C’,’D’.’F’].
1 comentario
Ahmed Khalifa
el 12 de Mayo de 2016
Walter Roberson
el 12 de Mayo de 2016
gradebounds = [0 80 90];
gradeletters = {'f', 'b', 'a'};
gradeidx = interp1(gradebounds, 1:length(gradebounds), x(:), 'previous', 'extrap');
grades = gradeletters(gradeidx);
Categorías
Más información sobre Operators and Elementary Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!