problem using num2str and bin2dec in size

5 ビュー (過去 30 日間)
ghattas akkad
ghattas akkad 2013 年 4 月 4 日
having an array of 32*32 when using num2str i get a char array of 32*94 why? i cannot use bin2dec on it
data = uint32(randi(2^31,[32,1]));
m = de2bi((data),32);
groups = num2str(m');
the array groups will be of type char 32*94 i cannot convert it to decimal :S
- - - Updated - - -
tic
data = uint32(randi(2^31,[32,1]));
m = de2bi((data),32);
groups = (m');
power2=[1,32];
for i=1:32
power2(i)=2^(i-1);
end
power2=uint32(power2);
decnum=[1,32];
for i=1:32
decnum(i)=(sum(power2.*groups(i,:)));
end
decnum=uint32(decnum);
toc
this is the updated code but it is somekind of slow if i want to do it on 500 000number any suggestions plz?

採用された回答

Iman Ansari
Iman Ansari 2013 年 4 月 5 日
Hi
groups(:,1:3:end) %makeing result 32*32 again
Try this(Your updated code):
data = uint32(randi(2^31,[32,1]));
m = de2bi((data),32);
groups = num2str(m');
a=groups(:,end:-3:1);
b=bin2dec(a);
c=uint32(b);

その他の回答 (3 件)

ghattas akkad
ghattas akkad 2013 年 4 月 5 日
thank you how did this do it ?
  1 件のコメント
Iman Ansari
Iman Ansari 2013 年 4 月 5 日
See this:
m = de2bi((4),8)
groups = num2str(m)
groups2 = groups(1,1:3:end)
the groups has two space between its numbers.

サインインしてコメントする。


ghattas akkad
ghattas akkad 2013 年 4 月 5 日
there is some problem the result of c in your code after the bin2dec is different then the ones i obtained in the array decnum
  5 件のコメント
Iman Ansari
Iman Ansari 2013 年 4 月 5 日
No:
clear
%%%%%mine
data = uint32(randi(2^31,[32,1]));
m = de2bi((data),32);
groups1 = num2str(m');
a=groups1(:,end:-3:1);
b=bin2dec(a);
c=uint32(b);
%%%%%%yours
groups = (m');
power2=[1,32];
for i=1:32
power2(i)=2^(i-1);
end
power2=uint32(power2);
decnum=[1,32];
for i=1:32
decnum(i)=(sum(power2.*groups(i,:)));
end
decnum=uint32(decnum);
mineVsyours=[c decnum']
ghattas akkad
ghattas akkad 2013 年 4 月 5 日
i think my code has a problem :) thank you :D

サインインしてコメントする。


ghattas akkad
ghattas akkad 2013 年 4 月 5 日
when update the code to look like this clear all close all
tic
data = uint32(randi(2^31,[500000,1]));
m = de2bi((data),32); groups = num2str(m'); a=groups(:,end:-3:1);
b=bin2dec(a); c=uint32(b);
toc
generate 500 000 numbers i get this error
Error using bin2dec (line 36) Binary string must be 52 bits or less.
Error in datatobin2 (line 12) b=bin2dec(a);
why ??
  3 件のコメント
ghattas akkad
ghattas akkad 2013 年 4 月 5 日
generate 500 000 numbers convert them to binary to group each set of bits together like a group of 1st bits = 1st row group of second bits=second row..... then convert them to decimal and send them to fpga using DMA to my sequential sorter
Iman Ansari
Iman Ansari 2013 年 4 月 5 日
500000 is very big but for lower number like 1000:
clear
data = uint32(randi(2^31,[1000,1]));
m = de2bi((data),32);
groups1 = num2str(m');
a=groups1(:,end:-3:1);
b=0;
for i=1:52:size(a,2)
b=b+bin2dec(a(:,i:min(i+51,end))).*2^(i-1);
end
But I think you shouldn't use c=uint32(b) after this.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by