You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to change element in base 10 to base 2 in a matrix, with loops and while
4 views (last 30 days)
Show older comments
Suppose i have a matrix called A,the elements ib base 10,how do i convert to base 2 .with conditions and loops.
14 Comments
yuval ohayon
on 9 Sep 2017
Edited: Cedric
on 9 Sep 2017
n=randi (6)
A=randi(100,n,n)
For (i=1:i < 100:i++)
Didnt know the 2nd base formula,it seems using sort or find func for reaching the element.the the formula to convert the element to 2nd base
John D'Errico
on 9 Sep 2017
Edited: John D'Errico
on 9 Sep 2017
What makes this not a doit4me? After all, it is trivial to use dec2bin, IF you just want to convert to base 2. Since you are not allowed to use dec2bin means it is homework. That you have apparently done nothing at all, this makes it a doit4me.
If you want help, then make an effort. How would you convert to base 2 if you were using pencil and paper? What, for example, would you do if you wanted to write the decimal number 27 in base 2? Once you understand the basic algorithm, it is easy to write code. But you need to make an effort on your homework.
Walter Roberson
on 9 Sep 2017
In MATLAB,
For (i=1:i < 100:i++)
would be written
for i = 1 : 99
However I do not see any connection between your question and any of the code you gave ?
yuval ohayon
on 11 Sep 2017
Can figure it out .is their destination veriable that need to get the 2nd base number after i convert it? .the algorithem is a loop ,divide the indexed element in 2,with a loop .helpp
Walter Roberson
on 11 Sep 2017
current_number = randi(32483209); %create an input
last_bit = mod(current_number, 2);
current_number = (current_number - last_bit)/2;
yuval ohayon
on 19 Sep 2017
Edited: Walter Roberson
on 19 Sep 2017
function [ ] = dectobin( current_num)
binary_num=CHAR(a)
while (current_num~=0)
last_bit=mod(current_num,2);
current_num=(current_num-last_bit)/2;
binary_num=lastbit;
this is the function ,i need help to finish it and return to main program
Walter Roberson
on 19 Sep 2017
What is a ?
I notice you are not recording the sequence of last_bit values.
I notice you are not returning any value from the function.
yuval ohayon
on 20 Sep 2017
Edited: Walter Roberson
on 20 Sep 2017
you are right, i need to return 1 variable, and i need recording the sequence i thought that's connect somehow to the char(a), its nonsense... but after i adding output variable, how do i record sequence of '1' '0' , AND HOW IN THE MAIN PROGRAM THE FUNC REPLACE FOR THE ALL MATRIX AND NT FOR SINGLE NUMBER
Walter Roberson
on 20 Sep 2017
Generally speaking:
count = 0;
variable = [];
while true
if I am not done yet
count = count + 1;
variable(count) = some value;
else
break;
end
end
In your situation you will probably need to reverse the order of the variable afterwards
yuval ohayon
on 24 Sep 2017
Edited: Walter Roberson
on 24 Sep 2017
o.k i understood that i need define a matrix (veriable in the example) and the places of each element is indexed by the 'count' but how i store the last bit and adding the next one until i get a string like '1001001',say binnum=1001001 veriavle(count)=binnum; and
function [y] = dectobin(current_num)
binary_num=0
while (current_num~=0)
last_bit=mod(current_num,2); %is the function is right?what i have missing?
current_num=(current_num-last_bit)/2;
binary_num=lastbit;
Walter Roberson
on 24 Sep 2017
variable(count) = lastbit;
Yes, mod(current_num,2) is fine to extract the last bit.
Answers (1)
Jose Marques
on 9 Sep 2017
A = magic(5) %generating a matrix 5x5
A = (A<10) %suppose you want the elements in A < 10
1 Comment
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)