I have the number 0.357. I want to multiply the number by 2 until it gets to a whole number. Once it gets to a whole number I want to subtract the whole number from 1 and then multiply by 2 until it becomes a whole number again. And I would like this to happen a specific number of times also.
  • This what I am trying to get my code to do. (Below)
0.357 *2 = 0.714
0.714 * 2 = 1.428
0.428 * 2 = 0.856
0.856 * 2 = 1.712
0.712 * 2 = 1.424
Thanks

2 Kommentare

James Tursa
James Tursa am 20 Feb. 2016
What have you done so far? Do you know how to program a for loop? Do you know how to program an infinite while loop?
WhatIsMatlab-
WhatIsMatlab- am 20 Feb. 2016
Bearbeitet: WhatIsMatlab- am 20 Feb. 2016
This is what I currently have. I am not sure I do. I want it to produce the values of m = [0 1 0 1 1]
m = [];
numbers = 0.357;
for i = 0:1:4
if numbers > 1
m = [m 1];
numbers = numbers - 1;
elseif numbers < 1
numbers= numbers*2;
m = [m 0];
end
end
display(m)

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Roger Stafford
Roger Stafford am 20 Feb. 2016
Bearbeitet: Roger Stafford am 20 Feb. 2016

0 Stimmen

For the precise number 0.357, which equals 357/(2^3*5^3), you can never arrive at an exact whole number by repeatedly multiplying by 2 since the prime factors 5^3 can never be cancelled out. However if you use matlab's double precision approximation for 0.357, you will actually arrive at a huge whole number at the end of 54 multiplications by 2.
Subtracting that whole number from 1 gives a negative number and multiplying a negative number by 2 will always result in another negative number, so you can never arrive at another whole number that way - whole numbers must be non-negative.
To carry out the first of the above steps, use a 'while' loop with a test for a whole number: floor(number)==number. (We know the results are always non-negative in this first step.)

7 Kommentare

WhatIsMatlab-
WhatIsMatlab- am 20 Feb. 2016
Bearbeitet: WhatIsMatlab- am 20 Feb. 2016
Could you give a little more information besides a while loop and a floor. I am very new to matlab. This is the code that I currently have now but it doesn't work correctly. It counts one number behind so instead of counting 0.856 it counts 0.428 has a zero value also. Which I don't want.
So the code should put out m = [0 1 0 1 1]
m = [];
numbers = 0.357;
for i = 0:1:4
if numbers > 1
m = [m 1];
numbers = numbers - 1;
elseif numbers < 1
numbers= numbers*2;
m = [m 0];
end
end
display(m)
% First step
n = 0.357;
c = 0; % The count
while floor(n)~=n
n = 2*n;
c = c + 1;
end
% You should exit here with c = 54 and an enormous whole value in n
WhatIsMatlab-
WhatIsMatlab- am 20 Feb. 2016
Bearbeitet: WhatIsMatlab- am 20 Feb. 2016
I can use that same process for what I want to do? I want to take 0.357 and multiply by 2. Since that number is great than 1. I want to subtract it by one. Then multiply by 2 again. Until I receive another number larger than 1. Then repeating this process. I provided my code above so you could see what I am trying to do.
Thanks
0.357 *2 = 0.714
0.714 * 2 = 1.428
0.428 * 2 = 0.856
0.856 * 2 = 1.712
0.712 * 2 = 1.424
I've taken a more careful look at your sample computations and corrected the code accordingly.
%First step
n = 0.357;
c = 0; % The count
while n ~= 0
n = 2*n;
n = n - floor(n); % Get fractional part of n
c = c + 1;
end
% You should still exit with c == 54.
WhatIsMatlab-
WhatIsMatlab- am 20 Feb. 2016
I am sorry but I do not see how this helps me. I am really trying to understand here. I just want my code to take a decimal number and return a 0 if the value is less than 1 and return a value of 1 if the number is greater than 1 after being multiplied. And then I want the process to continue on. Just like shown in my example.
Roger Stafford
Roger Stafford am 20 Feb. 2016
What I have shown you in the most recent comment is what you originally asked for and showed in your sample computations. Getting the string of 1's and 0's that you computed later is tantamount in this case to converting your original number to a binary fraction. It should be easy for you to modify the 'while' loop I gave you to produce that string. Just test that floor(n) when you obtain it before subtracting from n.
WhatIsMatlab-
WhatIsMatlab- am 20 Feb. 2016
Ok!! I think I see what you are saying now. Let me give it a go. And see what happens. I apologize, I was just having a hard time understanding before.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by