MATLAB: My code for isn’t working. To store iterations from a FOR loop into a single variable as a single row vector

arrayarraysfor loop

Error: in an assignment A(I) = B, the number of elements in B and I must be the same.
X = 100;
b = zeros(1,9);
for a = 2:9;
y = dec2base(x,a);
b(a)= y;
end
display(b)

Best Answer

Hi Moses
1.
add ; after end
2.
MATLAB is case sensitive, X capital is not used and then dec2base expects undeclared x, just use one of them:
3.
b is attempting to collect variable length results from dec2base, a fixed length variable cannot be used unless it has room for all digits. zeros(1,9) only allocates 1 digit per b(a), yet b elements need have variable length
.
x = 100;
b = {};
for a = 2:9;
b =[b; dec2base(x,a)];
% b(a)= y;
end
b =
'1100100'
'10201'
'1210'
'400'
'244'
'202'
'144'
'121'
to read cell elements
b{1}
=
1100100
b{3}
=
1210
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
Related Question