Matrix calculation(replacing a new value instead of matrix element)

1 回表示 (過去 30 日間)
shirin mhd
shirin mhd 2022 年 3 月 31 日
コメント済み: Voss 2022 年 3 月 31 日
Hi everyone
I've written this code. my purpose is to make a matrix(1*3), which each element (T1,T2,T3) of that is calculated in upper lines. But it doesn't work. what should i do?
a1=7;
a2=12;
a3=4;
w1=min([a1,a2,a3]);
f1=double(a1-w1);
f2=double(a2-w1);
f3=double(a3-w1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if f1==0
final=w1;
T1=-10e10;
else
T1=f1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if f2==0
final=w1;
T2=-10e10;
else
T2=f2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if f3==0
final=w1;
T3=-10e10;
else
T3=f3;
end
T=[T1,T2,T3]
i expect after run T becomes [3 8 -10e10]
but what code does is:
T =
1.0e+11 *
0.0000 0.0000 -1.0000
in other word, I want replace some elements in a matrix with new calculated value during some steps.which part is wrong?
I appreciate any help.

採用された回答

the cyclist
the cyclist 2022 年 3 月 31 日
The code is doing what you expect, but the format of the output obscures the result. Try using format long, and you'll see the result more clearly:
a1=7;
a2=12;
a3=4;
w1=min([a1,a2,a3]);
f1=double(a1-w1);
f2=double(a2-w1);
f3=double(a3-w1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if f1==0
final=w1;
T1=-10e10;
else
T1=f1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if f2==0
final=w1;
T2=-10e10;
else
T2=f2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if f3==0
final=w1;
T3=-10e10;
else
T3=f3;
end
format long
T=[T1,T2,T3]
T = 1×3
1.0e+11 * 0.000000000030000 0.000000000080000 -1.000000000000000
T1
T1 =
3
T2
T2 =
8
T3
T3 =
-1.000000000000000e+11
  1 件のコメント
shirin mhd
shirin mhd 2022 年 3 月 31 日
Thank you for your help and attention. I really appreciate your kindness.

サインインしてコメントする。

その他の回答 (2 件)

Voss
Voss 2022 年 3 月 31 日
The result T you get is correct. The confusion is because of how T is displayed in the Command Window. You can look at one element at a time to see them, or you can change how numbers are displayed in the Command Window:
Looking at individual elements of T:
T = [3 8 -10e10] % shows all of T, as-is
T = 1×3
1.0e+11 * 0.0000 0.0000 -1.0000
T(1)
ans = 3
T(2)
ans = 8
T(3)
ans = -1.0000e+11
Changing the Command WIndow format:
format long
T
T = 1×3
1.0e+11 * 0.000000000030000 0.000000000080000 -1.000000000000000
  2 件のコメント
shirin mhd
shirin mhd 2022 年 3 月 31 日
Thank you for your help and attention. I really appreciate your kindness.
Voss
Voss 2022 年 3 月 31 日
You're welcome!

サインインしてコメントする。


Image Analyst
Image Analyst 2022 年 3 月 31 日
Those are the values. It's just that when using 1e11 as the base, the 3 and 8 don't show in the display. To solve that put this at the top of your code
format long g
  2 件のコメント
Les Beckham
Les Beckham 2022 年 3 月 31 日
FWIW, I like format long e better so I don't have to count the zeros
shirin mhd
shirin mhd 2022 年 3 月 31 日
Thank you for your help and attention. I really appreciate your kindness.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by