Simplify Matrix decimal to integers

3 次查看(过去 30 天)
I do not want
881/2158 985/1393 780/1351
881/2158 -985/1393 780/1351
-881/1079 0 780/1351
But I want
1 1 1
1 -1 1
-2 0 1
How do I do this using one single function ?
I get this error if I use simplify
Check for incorrect argument data type or missing argument in call to function 'simplify'.

回答(2 个)

Alan Stevens
Alan Stevens 2021-10-19
编辑:Alan Stevens 2021-10-19
One way is
M = [881/2158 985/1393 780/1351
881/2158 -985/1393 780/1351
-881/1079 0 780/1351]
M = 3×3
0.4082 0.7071 0.5774 0.4082 -0.7071 0.5774 -0.8165 0 0.5774
ip = find(M>=0);
in = find(M<0);
M(ip) = ceil(M(ip));
M(in) = floor(M(in));
disp(M)
1 1 1 1 -1 1 -1 0 1
Don't know where you got the -2 from though!
  1 个评论
Shaunak Bagade
Shaunak Bagade 2021-10-19
I don't want to round the numbers ... I need to simplify it like 881/2158 is common factor in column 1 ... 985/1393 in column 2 ... 780/1351 in column 3

请先登录,再进行评论。


Cris LaPierre
Cris LaPierre 2021-10-19
编辑:Cris LaPierre 2021-10-19
It's likely going to take two functions: one to convert your symbolic expressions to numeric, and a second to round.
I would probably do something like
new_var = round(double(old_var))
  5 个评论
Steven Lord
Steven Lord 2021-10-19
Or you could normalize by the first element in each column.
M = [881/2158 985/1393 780/1351
881/2158 -985/1393 780/1351
-881/1079 0 780/1351];
B = normalize(M, 1, 'scale', 'first') % Normalize in dimension 1
B = 3×3
1 1 1 1 -1 1 -2 0 1
Shaunak Bagade
Shaunak Bagade 2021-10-19
编辑:Shaunak Bagade 2021-10-22
Thanks But now we know M but what if we need to normalize it when M is not known

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by