how can i print all the data from a computation in one line?

how can i do this? ex.
"for i=1:3
end"
desired output: i=[1 2 3] or i=123
..
ex.two
" x=input('input value of x: ') %then the user will enter x=[1:5]
y=x.*2;"
desired output: y=[2 4 6 8 10] or y=246810
..
ex three
N=35
while N>=2
A=rem(N,2)
N=floor(N/2)
end
and my desired output is A=11000 or A=[1 1 0 0 0]
how could i do this?

回答 (3 件)

Thomas
Thomas 2012 年 10 月 5 日
編集済み: Thomas 2012 年 10 月 5 日

0 投票

ii=1:3
or
ii=[1:3]

8 件のコメント

jelly
jelly 2012 年 10 月 5 日
thats not what i meant, i just made a simple example because im going to use this code for a more complicated one.
Image Analyst
Image Analyst 2012 年 10 月 5 日
Then what did you mean? Are you wanting to know how to use fprintf()?
jelly
jelly 2012 年 10 月 5 日
like this..
N=35
while N>=2
A=rem(N,2)
N=floor(N/2)
end
and my desired output is A=11000 or A=[1 1 0 0 0]
Image Analyst
Image Analyst 2012 年 10 月 5 日
Like this:
>> N=35
N =
35
>> nBinary = dec2bin(N)
nBinary =
100011
>> nBinaryReversed = nBinary(end:-1:1)
nBinaryReversed =
110001
jelly
jelly 2012 年 10 月 5 日
編集済み: jelly 2012 年 10 月 5 日
ughhh, you just dont get me, haha, well any way, thanks for bothering to answer :).
Thomas
Thomas 2012 年 10 月 5 日
I guess this is what u need
N=35;
count=1;
while N>=2
A(count)=rem(N,2);
N=floor(N/2);
count =count+1;
end
A
jelly
jelly 2012 年 10 月 6 日
Hey tom, yes this is really what i need, finally someone get what i mean, HAHA, thankyou for spending time understanding my problem, thanks bro.
Walter Roberson
Walter Roberson 2012 年 10 月 6 日
You will find that this code will not print the result on one line at some point around 2^19 to 2^24 (depending exactly how wide your command window is set to.) Instead you will get a display such as
A =
Columns 1 through 18
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 19 through 20
0 0
The output for A will also not have the [] that you required when there were spaces between the elements.
You can fix both of those problems by not displaying A and instead displaying
mat2str(A)

サインインしてコメントする。

Image Analyst
Image Analyst 2012 年 10 月 5 日

0 投票

If you're going to allow the user to enter brackets and colons, then you're going to have to accept it as a string and either call eval(), or parse the string to extract out the individual numbers.
Walter Roberson
Walter Roberson 2012 年 10 月 5 日

0 投票

N=35
fprintf('%s', 'A=');
while N>=2
A=rem(N,2);
fprintf('%d', A);
N=floor(N/2);
end
fprintf('\n')
Note that your logic will fail to print the last binary digit.

カテゴリ

ヘルプ センター および File ExchangeSimulink についてさらに検索

タグ

タグが未入力です。

質問済み:

2012 年 10 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by