Create an array based on another array's input

조회 수: 4 (최근 30일)
Ahmed Khalifa
Ahmed Khalifa 2016년 5월 11일
답변: Walter Roberson 2016년 5월 12일
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.

답변 (2개)

Roger Stafford
Roger Stafford 2016년 5월 11일
Use the ‘histc’ function, [~,ix] = histc(x,grade_ranges), then use ix to index into a vector [‘A’,’B’,’C’,’D’.’F’].
  댓글 수: 1
Ahmed Khalifa
Ahmed Khalifa 2016년 5월 12일
I really cant understand this function at all

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2016년 5월 12일
gradebounds = [0 80 90];
gradeletters = {'f', 'b', 'a'};
gradeidx = interp1(gradebounds, 1:length(gradebounds), x(:), 'previous', 'extrap');
grades = gradeletters(gradeidx);

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by