input=[1 1 0 1 0 0 1 0 ]; %input bit
x1=bin2dec(num2str(11)); %converting number to characters
x2=bin2dec(num2str(01));
x3=bin2dec(num2str(00));
x4=bin2dec(num2str(10));
xx=[x1,x2,x3,x4];
This is my code, I don't want to use individual bin2dec conversion. How can I put it in 'for loop'?

 採用された回答

Stephen23
Stephen23 2020 年 5 月 30 日
編集済み: Stephen23 2020 年 5 月 30 日

0 投票

This is MATLAB, you don't need a for loop!
Method one: mtimes:
>> V = [1,1,0,1,0,0,1,0];
>> N = 2;
>> X = pow2(N-1:-1:0)*reshape(V,N,[])
X =
3 1 0 2
Method two: bin2dec:
>> V = [1,1,0,1,0,0,1,0];
>> N = 2;
>> X = bin2dec(reshape(sprintf('%u',V),N,[]).');
X =
3
1
0
2

3 件のコメント

Darshan Manjunathrao Chawan
Darshan Manjunathrao Chawan 2020 年 5 月 30 日
編集済み: Darshan Manjunathrao Chawan 2020 年 5 月 30 日
But can't I put it in for loop to determine individual values. I need to understand properly, since I am a beginner.
Stephen23
Stephen23 2020 年 5 月 30 日
"But can't I put it in for loop to determine individual values"
Of course, you can use a loop if you want to. Try it.
Darshan Manjunathrao Chawan
Darshan Manjunathrao Chawan 2020 年 5 月 30 日
Can you please help me with that?

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by