Convert binary column vector to decimal

2 views (last 30 days)
I'm trying to convert each column of a matrix A of ones and zeros to their corresponding decimal value and sort the decimal values in a new (1xn) matrix B. For eg. A=[100;011,101] B=[5 2 3] This is for a matrix A of size 52x15 and matrix B of size 1x15.

Accepted Answer

Eduardo Márquez
Eduardo Márquez on 11 Feb 2015
A simple way can be:
close all; clear all; clc
% Created matrix with 0 and 1
matrix = rand(52,15) ;
matrix(matrix<=0.5) = 0;
matrix(matrix>=0.5) = 1;
% Save memory
decim = zeros(1,size(matrix,2));
% Begin string empty
S=[];
% For each column
for j = 1:size(matrix,2)
% Get the column
column = matrix(:,j)';
% Concatenate the numbers
for i = 1:size(matrix,1)
S = [S num2str(column(i))];
end
% Save the value dec
decim(1,j) = bin2dec(S);
% Empty String
S=[];
end
% Sorted tge results
sorted = sort(decim);
I hope to help.
  1 Comment
Stephen23
Stephen23 on 12 Feb 2015
Edited: Stephen23 on 12 Feb 2015
This is poor MATLAB code.
In particular it:
  • uses multiple nested loops, which does not utilize MATLAB's vectorization abilities.
  • concatenates and extends the string S with every iteration, without any preallocation . This is slow as MATLAB needs to reallocate memory on every iteration. In this code it is twice as bad, as the outer loop also resets S to [] on every iteration.
  • Calls unnecessary and slow num2str on every loop: the data can be kept numeric, which is simpler and faster.
  • Using i and j for the names of the loop variables. These are the names of the inbuilt imaginary unit .
For much faster, neater, and more compact solutions, consider using either of the other two provided answers.

Sign in to comment.

More Answers (2)

Stephen23
Stephen23 on 11 Feb 2015
Edited: Stephen23 on 11 Feb 2015
Note that A=[100;011,101] generates an error in MATLAB due to inconsistent dimensions. Perhaps you meant [100;011;101]? It is also important to realize that this defines decimal numbers 100, etc, not a list of binary digits. This means you are limited to about 15 digits (if using the default double data class). A better solution is to store the digits separately, either in a numeric or a char array.
If you store the digits as decimal numbers, try this code:
>> A = [100,011;101,111];
>> B = strjust(char(arrayfun(@(n)sprintf('%d',n),A(:), 'UniformOutput',false)))-'0';
>> B(B<0) = 0;
>> C = reshape(sum(bsxfun(@pow2,B,size(B,2)-1:-1:0),2),size(A))
C =
4 3
5 7
If you really do have a matrix of binary digits, not decimal numbers as shown in the question, then you can use this much simpler version:
D = [1,0,0;0,1,1;1,0,1;1,1,1]; % each row = one binary number
E = sum(bsxfun(@pow2,D,size(D,2)-1:-1:0),2);
If you decide to store your digits in strings, then you could use bin2dec, or this:
F = ['100';'011';'101';'111']; % each row = one binary number
G = sum(bsxfun(@pow2,F-'0',size(F,2)-1:-1:0),2);

James Tursa
James Tursa on 11 Feb 2015
If I understand your question, A is a numeric 52 x 15 matrix consisting of 1's and 0's. For that case you can use:
B = bin2dec(char(A'+'0'))'
If A is actually char data and not numeric, then it is just:
B = bin2dec(A')'
I don't understand what your comment "sort the decimal values" means, since your given result B is not sorted. Maybe you meant to type "store the decimal values"?
  3 Comments
James Tursa
James Tursa on 12 Feb 2015
OK, I don't care about the Accepted Answer points, but I am curious why you accepted the double-for-loop solution which explicitly converts each digit separately instead of my one-liner solution which does everything at once?
Eduardo Márquez
Eduardo Márquez on 12 Feb 2015
I'm new user, I think that seek to understand how the code works, as you say your answer is more efficient, but being new user would not understand at all.
Sorry, my English is not that great.

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices 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!