i have an array wr(i,j) that is 256X8 in binary. now i need to convert it to decimal. so after conversion the size should be 16x16. i am using bi2de() function but i am unsuccessful to do it. how can i do it?

 Respuesta aceptada

Guillaume
Guillaume el 17 de Sept. de 2015
Editada: Guillaume el 17 de Sept. de 2015

0 votos

Note that in theory, conversions to string and back are much slower than purely manipulating numbers. With matlab treating numbers as double by default, it may not save you anything but I would consider bypassing the usage of strings and dec2bin altogether. The code would be:
wr = false(256, 8); %predeclare array for better performance
for row = 1:256 %avoid using i and j, they're functions defined by matlab
for col = 1:8
wr(row, col) = round(RU(4, 4, m)) == calc; %no need for if either. just logical assignment
end
end
wr = sum(bsxfun(@times, 2.^(7:-1:0), wr), 2);
wr = reshape(wr, [16 16]); %not sure why you want to do that.

4 comentarios

anika hossain
anika hossain el 17 de Sept. de 2015
Editada: anika hossain el 17 de Sept. de 2015
not working. i think you misinterpret my description.
if round(RU(4,4,m))==calc
wr(i,j)='1';
else
wr(i,j)='0';
end
wr(i,j) is not equal to round(RU(4,4,m)). it is a character 1/0,you can say i am saving it as a character. & its a image which's pixel values were converted to binary value for working & now i need to go back to the pixel value by converting it to decimal.
& your code did not work for me...
Guillaume
Guillaume el 17 de Sept. de 2015
In my code above, wr(i,j) is not equal to round(RU(4,4,m), it is equal to round(RU(4,4,m))==calc which is a completely different thing and can only have for values logical 0 or logical 1.
The line of code
wr(row, col) = round(RU(4, 4, m)) == calc;
is exactly equivalent to:
if round(RU(4,4,m))==calc
wr(row,col) = 1; %note that it is a number (logical) not a char
else
wr(row,col) = 0
end
You will have to explain better what is not working with my code
anika hossain
anika hossain el 17 de Sept. de 2015
as i said, i was saving 1/0 as char in wr(row,col), thats why your code did not work for me. now as per your explanation i am saving it as number and your code works perfectly. thanks
Guillaume
Guillaume el 17 de Sept. de 2015
Well, the whole idea is to never go near a char, as string manipulations are typically order of magnitude slower than just numbers.

Iniciar sesión para comentar.

Más respuestas (1)

Kirby Fears
Kirby Fears el 16 de Sept. de 2015
Editada: Kirby Fears el 16 de Sept. de 2015

0 votos

Hi Anika,
I used the following code to convert a 16x16 matrix to binary (256x8) and back with no problems. Please check that you are calling the bin2dec function correctly and wr(i,j) is a "char" array of 1's and 0's. It would help if you post your code, your binary array, and your error message.
m=225*ones(16);
mb=dec2bin(m);
m2=bin2dec(mb);

6 comentarios

anika hossain
anika hossain el 16 de Sept. de 2015
Editada: anika hossain el 16 de Sept. de 2015
my array is like-
if round(BU(4,4,m))==calc
wb(i,j)=char(1);
else
wb(i,j)=char(0);
end
the error message-
??? Error using ==> bin2dec at 54
Binary string may consist only of characters 0 and 1
Error in ==> second at 123
m2=bin2dec(wr)
Kirby Fears
Kirby Fears el 16 de Sept. de 2015
The char function does not do what you think it does. char(1) returns the first character in the Matlab dictionary of characters, not 1 as a character. You can read the documentation here. Specifically, here is an example from the documentation:
Example: char(65) converts the integer 65 into the character A.
To fix your problem, you can simply recode as follows:
if round(BU(4,4,m))==calc
wb(i,j)='1';
else
wb(i,j)='0';
end
end
Hope this helps.
anika hossain
anika hossain el 16 de Sept. de 2015
yes,this problem solved. but i think i am doing some more wrong things.
for i=1:256
for j=1:8
if round(RU(4,4,m))==calc
wr(i,j)='1';
else
wr(i,j)='0';
end
end
end
wr=bin2dec(wr)
now its converting it to binary but wr is 256x1 double array after that.
Walter Roberson
Walter Roberson el 17 de Sept. de 2015
And the 256 x 1 double array cannot be reshaped?
anika hossain
anika hossain el 17 de Sept. de 2015
should not the 256x8 char array be converted to 16x16 array after using 'bin2dec()' function? i thought i could do that, if what to do?
Walter Roberson
Walter Roberson el 17 de Sept. de 2015
No, bin2dec converts N x M arrays to column arrays of length N. You need to reshape() the result to whatever size you want.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Preguntada:

el 16 de Sept. de 2015

Comentada:

el 17 de Sept. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by