fill a matrix with binary code in a for loop

Hi,
in a for-loop I get 30 times 3 bits each. To display the bits in a vector is no problem for me. But I would like to have a matrix after the 30 passes. Unfortunately my approach does not succeed.
for i = 1:30
% I reveive:
bit1 = [0]
bit2 = [1]
bit3 = [1]
% create Vektor works fine:
bitcode = [bit1 bit2 bit3]
% create Matrix doesnt work:
bitcode(i, :) = [bit1 bit2 bit3]
% neither that way:
for k = 1:length(bitcode)
bitcode(i,k) = bit(k)
end
end
Result should look like:
bitcode = [0 1 1; 1 1 1; 1 0 1]
I guess I'm doing something wrong?

3 件のコメント

David Fletcher
David Fletcher 2021 年 5 月 9 日
編集済み: David Fletcher 2021 年 5 月 9 日
I'm not sure how you expect to get a 3 row matrix when you are using a row indexing loop that iterates 30 times to create the matrix, nor am I sure how you expect each row to be different when it is based on the same arrangement of bits. However as a general example of using a loop to create a matrix, this might be of help:
code = [0 1 1]
for k = 1:length(bitcode)
bitcode(k,:) = code
end
Christian
Christian 2021 年 5 月 10 日
編集済み: Christian 2021 年 5 月 10 日
thank you! you are absolutely right. logic is sometimes not very strong with me. your solution works, thank you very much!:)
(@David Fletcher: I would like to confirm your statement as an answer, unfortunately I can't do that since you used the comment function)
David Fletcher
David Fletcher 2021 年 5 月 10 日
Ok, I'll post it as an answer if it helped you, but it's not a big deal to me

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

 採用された回答

David Fletcher
David Fletcher 2021 年 5 月 10 日

1 投票

code = [0 1 1]
for k = 1:length(bitcode)
bitcode(k,:) = code
end

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by