For this loop I want the output to print str='b' anytime A='1'

1 vue (au cours des 30 derniers jours)
Ernest Adamtey
Ernest Adamtey le 6 Août 2021
Réponse apportée : Rik le 6 Août 2021
For this loop I want the output to print str='b' anytime A='1'
clc
clear
for q=20:21
str=[];
A=dec2base(q,19)
if A=='1'
str=[str sprintf('b')]
end
str
end
but when A=11 the output is 'b' instead of 'bb'. Please help

Réponses (1)

Rik
Rik le 6 Août 2021
This behaviour is exactly as documented. if A=='1' does not create a loop.
clc
clear
str=[];
for q=20:21
A=dec2base(q,19);
str_=repmat('b',1,sum(A=='1'));
fprintf('%s results in %s\n',A,str_)
str=[str str_];
end
11 results in bb 12 results in b
str
str = 'bbb'

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by