Why does my else doesnt work?

2 visualizzazioni (ultimi 30 giorni)
Debbie
Debbie il 15 Nov 2022
Please help. I'm learning how to use matlab on my own but I don't know why my if-else code won't work.
>> a = [-3.821; 0.135; 5.995; -5.557; -4.041; -3.094; -3.244; -3.074; -1.241; -4.216; -2.834; -0.424; 5.337; -0.088; 2.985; 4.136; 1.939; 6.031; -2.607; 3.340]
a =
-3.8210
0.1350
5.9950
-5.5570
-4.0410
-3.0940
-3.2440
-3.0740
-1.2410
-4.2160
-2.8340
-0.4240
5.3370
-0.0880
2.9850
4.1360
1.9390
6.0310
-2.6070
3.3400
>> b = [-1.529; -2.882; 1.287; 3.822; 0.422; -4.387; 3.107; -3.513; 4.760; -5.205; 2.265; -4.442; 6.247; -2.788; -2.688; -1.761; 1.987; 4.484; 0.052; -2.763]
b =
-1.5290
-2.8820
1.2870
3.8220
0.4220
-4.3870
3.1070
-3.5130
4.7600
-5.2050
2.2650
-4.4420
6.2470
-2.7880
-2.6880
-1.7610
1.9870
4.4840
0.0520
-2.7630
>> c = (a.^2 + b.^2).^(1/2)
c =
4.1156
2.8852
6.1316
6.7445
4.0630
5.3683
4.4919
4.6680
4.9191
6.6983
3.6279
4.4622
8.2164
2.7894
4.0169
4.4953
2.7763
7.5153
2.6075
4.3347
>> d = atand(a./b)
d =
68.1908
-2.6819
77.8837
-55.4805
-84.0382
35.1940
-46.2358
41.1871
-14.6125
39.0071
-51.3673
5.4525
40.5083
1.8079
-47.9969
-66.9370
44.2995
53.3695
-88.8573
-50.4009
if (d<0)
e= 360-d;
else
e==d
end

Risposta accettata

David Hill
David Hill il 15 Nov 2022
a = [-3.821; 0.135; 5.995; -5.557; -4.041; -3.094; -3.244; -3.074; -1.241; -4.216; -2.834; -0.424; 5.337; -0.088; 2.985; 4.136; 1.939; 6.031; -2.607; 3.340];
b = [-1.529; -2.882; 1.287; 3.822; 0.422; -4.387; 3.107; -3.513; 4.760; -5.205; 2.265; -4.442; 6.247; -2.788; -2.688; -1.761; 1.987; 4.484; 0.052; -2.763];
c = (a.^2 + b.^2).^(1/2);
d = atand(a./b)
d = 20×1
68.1908 -2.6819 77.8837 -55.4805 -84.0382 35.1940 -46.2358 41.1871 -14.6125 39.0071
e = d;
e(d<0)=360-d(d<0)
e = 20×1
68.1908 362.6819 77.8837 415.4805 444.0382 35.1940 406.2358 41.1871 374.6125 39.0071

Più risposte (2)

Chandler Hall
Chandler Hall il 15 Nov 2022
You can use conditional statements to modify a variable directly, known as logical indexing. Try:
d(d < 0) = 360 + d(d < 0)
See here or here for more examples.

RAGHUNATHRAJU DASHARATHA
RAGHUNATHRAJU DASHARATHA il 15 Nov 2022
Modificato: RAGHUNATHRAJU DASHARATHA il 15 Nov 2022
As per my understanding you are using conditional statement if-else to get your output but unable to do it because in else condition you wrote e==d which does the element by element comparisons and returns an array with elements set to logical 1 (True) where the relation is true and elements set to logical 0(False) when the relation is false.
you can just replace it by e=d instead of e==d
a = [-3.821; 0.135; 5.995; -5.557; -4.041; -3.094; -3.244; -3.074; -1.241; -4.216; -2.834; -0.424; 5.337; -0.088; 2.985; 4.136; 1.939; 6.031; -2.607; 3.340];
b = [-1.529; -2.882; 1.287; 3.822; 0.422; -4.387; 3.107; -3.513; 4.760; -5.205; 2.265; -4.442; 6.247; -2.788; -2.688; -1.761; 1.987; 4.484; 0.052; -2.763];
c = (a.^2 + b.^2).^(1/2);
d = atand(a./b)
d = 20×1
68.1908 -2.6819 77.8837 -55.4805 -84.0382 35.1940 -46.2358 41.1871 -14.6125 39.0071
if d<0
e= 360-d;
else
e=d
end
e = 20×1
68.1908 -2.6819 77.8837 -55.4805 -84.0382 35.1940 -46.2358 41.1871 -14.6125 39.0071
for further more refer the following link .

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by