How to change element in base 10 to base 2 in a matrix, with loops and while

2 次查看(过去 30 天)
Suppose i have a matrix called A,the elements ib base 10,how do i convert to base 2 .with conditions and loops.
  14 个评论
yuval ohayon
yuval ohayon 2017-9-24
编辑:Walter Roberson 2017-9-24
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;

请先登录,再进行评论。

回答(1 个)

Jose Marques
Jose Marques 2017-9-9
A = magic(5) %generating a matrix 5x5
A = (A<10) %suppose you want the elements in A < 10

类别

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