How to replace certain array values ​​using if, for or while?

1 Ansicht (letzte 30 Tage)
Brenno Selli
Brenno Selli am 8 Mär. 2023
Bearbeitet: Arka am 8 Mär. 2023
I have this 45x10 array with vallues between 0 and 2.5, I want to replace those values with letters if certain conditions are being atended.
The conditions are:
If the value is greater than 1.6, it gets grade E... if the value is greater than 1 and less than 1.6, it will receive a D grade... If the value is greater than 0.63 and less than 1, it receives a grade of C... If the value is greater than 0.315 and less than 0.63, it receives a grade of B...And finally, if the value is less than 0.315, it receives an A grade.
It can also be grades between 2 and 10 following the same ones I showed earlier (E=2, D=4, C=6, B=8 and A=10), but it would be better if the letters were used.

Akzeptierte Antwort

Arka
Arka am 8 Mär. 2023
Bearbeitet: Arka am 8 Mär. 2023
Hi,
You can use conditional indexing to achieve this.
This is the MATLAB code for the same:
scores = [0.2200 0.2946 0.3848 0.4869 0.5895 0.6857 0.7750 0.8591 0.9400 1.0192;
0.3489 0.4471 0.5495 0.6561 0.7580 0.8507 0.9363 1.0182 1.0984 1.1775;
0.3319 0.4348 0.5357 0.6343 0.7288 0.8186 0.9048 0.9892 1.0732 1.1566;
0.2766 0.3724 0.4812 0.5927 0.6978 0.7948 0.8862 0.9756 1.0657 1.1583;
0.2744 0.3508 0.4232 0.4876 0.5453 0.5983 0.6478 0.6950 0.7404 0.7843;
0.2084 0.2623 0.3129 0.3615 0.4092 0.4555 0.4989 0.5384 0.5739 0.6064;
0.2715 0.3434 0.4023 0.4543 0.5060 0.5596 0.6140 0.6672 0.7171 0.7626;
0.1909 0.2539 0.3181 0.3812 0.4419 0.4991 0.5526 0.6032 0.6521 0.7007;
0.1993 0.2523 0.3038 0.3547 0.4052 0.4549 0.5045 0.5555 0.6095 0.6667;
0.3176 0.3967 0.4696 0.5367 0.5995 0.6587 0.7161 0.7749 0.8382 0.9075;
0.2000 0.2609 0.3223 0.3814 0.4367 0.4880 0.5360 0.5830 0.6314 0.6824;
0.3302 0.4083 0.4813 0.5507 0.6168 0.6790 0.7392 0.8011 0.8678 0.9404;
0.3534 0.4301 0.5064 0.5810 0.6526 0.7233 0.7956 0.8706 0.9491 1.0313;
0.3786 0.4476 0.5026 0.5516 0.6003 0.6532 0.7126 0.7784 0.8489 0.9222;
0.3516 0.4468 0.5364 0.6223 0.7031 0.7767 0.8434 0.9051 0.9635 1.0191];
grades = zeros(size(scores));
grades(scores > 1.6) = 'E';
grades((scores > 1) & (scores <= 1.6)) = 'D';
grades((scores > 0.63) & (scores <= 1)) = 'C';
grades((scores > 0.315) & (scores <= 0.63)) = 'B';
grades(scores < 0.315) = 'A';
scores = char(grades)
Now you can access individual grades using matrix indexing. To access the grade in the ith row and jth column:
scores(i,j)
To learn more about conditional indexing, please refer to the MathWorks documentation below:

Weitere Antworten (0)

Kategorien

Mehr zu Downloads finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by