My code for isn't working. To store iterations from a FOR loop into a single variable as a single row vector
1 view (last 30 days)
Show older comments
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)
0 Comments
Accepted Answer
John BG
on 10 Mar 2017
Edited: John BG
on 10 Mar 2017
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
2 Comments
John BG
on 10 Mar 2017
Edited: John BG
on 10 Mar 2017
ok,
y=dec2base(x,a)
is exactly the same number expressed in different bases.
if you want to store in vector b, scalars, you cannot put in b, same vector '22' and '1100100', which are the same figure, unless they're expressed on the same base, that then
b = [0 x x x x x x x x]
b the way you wanted in the question, is constant, isn't it?
if you want to plot b, as simple as
b=x*ones(1,8)
b=[0 b] % adding the initial zero you put
stem(b)
or
plot(b)
it's flat because b([2:9]) is x
regards
John BG
More Answers (0)
See Also
Categories
Find more on Logical 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!