difference ./ and / with constant

14 次查看(过去 30 天)
Hi,
I've noticed something strange and I want to understand why this is the way it is. Simple example: take a random matrix A = [0.1234;0.5678;0.9012], the I divide this by the same constant c = 1.225 in two ways: ./ (right array division) and / (regular division).
B = A ./ c
C = A / c
The result they each give seems the same (at first glance):
0.1007
0.4635
0.7357
But when I use a simple loop (see further) to determine the number of significant digits after the comma, so before the trailing zeros, they are not the same. The last logical test with the round functions that passes is:
N = 15;
round(B(1,1),N) == C(1,1)
Starting at N = 14 and lower, the logic test fails. So this means that one of both operators (I'm guessing the ./) fixes the significance at 14 digits after the comma.
My question: Why is this and how do I get around this?
Loop to determine the significance:
function [ sigdig ] = signDig( commaNumber )
% Find the significant digits after the comma
% » count backwards to finish with the smallest N value possible
% » till -100 included
% Interpretation:
% N > 0: significanct to N digits to the right of the decimal point.
% N = 0: significanct to the nearest integer.
% N < 0: significanct to N digits to the left of the decimal point.
% If array, it will loop all commaNumbers and return the max significance
% If single value, it will return the sign of that value
for i = 1:length(commaNumber)
for N = 100:-1:-100
if round(commaNumber(i),N) == commaNumber(i)
sigdig(i,1) = N;
end
end
end
sigdig = max(sigdig);
end
  1 个评论
Stephen23
Stephen23 2018-8-9
编辑:Stephen23 2018-8-9
"./ (right array division) and / (regular division)"
Actually the functions are:
  • ./ rdivide, described as "Right array division".
  • / mrdivide, described as "Solve systems of linear equations"
It should be pretty clear from the description which one you want to use: if you are not solving equations, then right array division is better, simply because it makes the intent of your code clearer.
The differences between array and matrix operations are described here:
In general if you are not doing linear algebra then you should be using the dotted operators.
Note the mrdivide documentation states clearly that "If A is a scalar, then B/A is equivalent to B./A".

请先登录,再进行评论。

采纳的回答

Ben Samyn
Ben Samyn 2018-8-9
OK, I guess it's a bit late here (5 a.m.) and I should go to bed. I solved my own problem by using
format long
The result of the ./ and / division remains the same, e.g.:
B(1,1) = 0.100734693877551
C(1,1) = 0.100734693877551
and
B(1,1) == C(1,1)
does yield a logical true.
It is also quite logical that e.g.:
N = 12;
round(B(1,1),N) == C(1,1)
yields a logical false, as this is actually
0.100734693878 == 0.100734693877551
which of course is not equal.
I answered my own question for anyone who might be as confused as I was. Sorry to waste your time.
Goodnight

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by