Can you guys help me with this one?
Thanks

댓글 수: 1

Roger Stafford
Roger Stafford 2017년 2월 2일
편집: Roger Stafford 2017년 2월 2일
In case it is a mystery to you why the described percentage is not one hundred percent, it should be realized that if x is any integer other than a power of two, the reciprocal 1/x cannot be achieved exactly in the binary numbers, which your computer uses, with a finite number of binary digits and must therefore approximate that reciprocal by rounding. It is then a matter of chance whether the multiplication x*(1/x) will turn out to be exactly one or off by some tiny amount.

댓글을 달려면 로그인하십시오.

 채택된 답변

Star Strider
Star Strider 2017년 2월 2일

1 개 추천

It seems straightforward. Use the ‘double equal’ operator == to test for equality.
A 1 means the condition is met, a 0 indicates it was not. To understand the nature of the problem (if it’s not already been explained to you) see Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero? (link).

댓글 수: 7

John Jamison
John Jamison 2017년 2월 2일
so how would i get the code to calculate how many = 1 and how many = 0?
Star Strider
Star Strider 2017년 2월 2일
편집: Star Strider 2017년 2월 2일
The equality test will produce a logical 1 if the test is met and a logical 0 if it is not. In MATLAB, logical values become double-precision variables if you perform arithmetic operations on them. The sum function will work to sum the vector, so the result will be the number of times the condition is met. Divide that by 1000 to get the percentage (or by 100000 to get the decimal fraction) of times the condition was met. The number of 0 values is simply 100000 minus the number of 1 values. You can expand on that idea to get the percentage of 0 values in your experiment.
—————
EDIT I get the same result Image Analyst gets:
N = 100000;
Test = [1:N].*(1./[1:N]);
Passed = sum(Test == 1);
Passed_Pct = Passed/1000
Passed_Pct =
86.884
John Jamison
John Jamison 2017년 2월 2일
Why do you multiply 1:N times the equation? Why can't you just use the equation: ie. (1 ./ 1:N)?
Stephen23
Stephen23 2017년 2월 2일
편집: Stephen23 2017년 2월 2일
@John Jamison: if you consider it mathematically this code:
[1:N].*(1./[1:N])
will create a vector of ones. But because floating point calculations accumulate errors, not all of the values will be ==1 when that code is calculated numerically by a computer. Therefore that code allows a simple estimate of what proportion of rational fractions within that range can be multiplied by their inverse (i.e. the integer) to be ==1.
Star Strider
Star Strider 2017년 2월 2일
Thank you, Stephen.
The vector code I used replaces an explicit for loop that would do essentially the same calculation, as the similarity of Image Analyst’s and my code results demonstrate.
John Jamison
John Jamison 2017년 2월 3일
I still don't get why its not just (1 ./ 1:N)?
thanks
Star Strider
Star Strider 2017년 2월 3일
The problem in the ‘Screen Shot 2017-02-01 at 5.55.24 PM.png’ that you posted wants to calculate: x*(1/x) and test to see if it equals 1. That is the reason it’s not just 1./[1:N].

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2017년 2월 2일

1 개 추천

How about a simple for loop? Here's a start
equalityCount = 0;
for i = 1 : 100000
if i * (1/i) == 1
I trust you can finish it. I get 86884 = 86.884% Empirically it seems to be higher than the theoretical number Roger suggests. Not sure why.
>> format long
>> 17 * (1/17)
ans =
1
>> 17 * (1/17) == 1
ans =
logical
1

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2017년 2월 1일

댓글:

2017년 2월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by