MATLAB: How to get the for loop to run correctly

for loopnum2str

A = [1, 2, 3;4,5,6];
result = zeros(size(A));
for k = [1:2]
result = strcat(num2str(A(k,1)),num2str(A(k,2)),num2str(A(k,3)));
k = k+1;
end
The aim of the above code is to combine three single digits to one 3-digit number. The result I expect is [123;456] but instead I am getting only [456]. Also when i check the value of k after running it, it says k =2. Please what is wrong with my code?

Best Answer

out = A*10.^(size(A,2)-1:-1:0)'; % in your case
other [EDIT]
n = floor(log10(A(:,2:end)))+1;
n = [n, zeros(size(A,1),1)];
out = sum(A.*10.^cumsum(n,2,'r'),2);
% using
>> A =[ 60 110 49
17 9 91];
>> n = floor(log10(A(:,2:end)))+1;
n = [n, zeros(size(A,1),1)];
out = sum(A.*10.^cumsum(n,2,'r'),2)
out =
6011049
17991
>>
or
out = mat2cell(sprintf('%d',A'),1,sum(floor(log10(A))+1,2)')';
Related Question