If statement for matrix

4 vues (au cours des 30 derniers jours)
Sewwandhi Chandrasekara
Sewwandhi Chandrasekara le 4 Avr 2022
Modifié(e) : DGM le 5 Avr 2022
I have a matrix of 73 rows andd 12 columns having different values to each cell. I want to assign if cell value=0.5 display "0, else if cell value >=0.5 display "+1" else cell value<=0.5 display "-1". I tried following, but gives only one value.1,1 cell value is -0.5438.
if B==0.5;
disp='0';
elseif B>=0.5;
disp='+1';
else
disp='-1'
end

Réponses (1)

DGM
DGM le 5 Avr 2022
Modifié(e) : DGM le 5 Avr 2022
If you want to change array values, why are you using disp()?
This will produce the output of the same size:
A = rand(4,3); % a test array
A(1) = 0.5 % make sure at least one is exactly 0.5
A = 4×3
0.5000 0.4352 0.4317 0.2511 0.4596 0.8072 0.9170 0.0056 0.6738 0.1956 0.2177 0.4465
B = sign(A-0.5) % simple numeric output
B = 4×3
0 -1 -1 -1 -1 1 1 -1 1 -1 -1 -1
If for some reason, you want a bunch of text output:
cmap = {'-1','0','+1'};
Btext = cmap(sign(A-0.5)+2) % cellchar output
Btext = 4×3 cell array
{'0' } {'-1'} {'-1'} {'-1'} {'-1'} {'+1'} {'+1'} {'-1'} {'+1'} {'-1'} {'-1'} {'-1'}

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by