Reputation: 659
I am trying to concatenate 0s and 1s in a matrix to form a binary number (as a string).
For example, [1 0 1;0 0 1]
should output ['101';'001']
When trying this input however, I get ['1','1']
as a result. Why?
function result = generateBinary(ref_matrix)
[row col] = size(ref_matrix);
result = cell(1,row);
str = '';
for i=1:row
for j = 1:col
n = num2str(ref_matrix(i,j))
str = strcat(str, num2str(ref_matrix(i,j)));
str
result{1,i} = str;
str = '';
end
end
end
Upvotes: 1
Views: 26
Reputation: 36710
The first end
is at the wrong place.
function result = generateBinary(ref_matrix)
[row col] = size(ref_matrix);
result = cell(1,row);
str = '';
for i=1:row
for j = 1:col
n = num2str(ref_matrix(i,j))
str = strcat(str, num2str(ref_matrix(i,j)));
str
end
result{1,i} = str;
str = '';
end
As the indent suggests, result{1,i} = str;str = '';
may not be part of the inner loop.
Upvotes: 1