How to create a cell array with binary numbers?
3 次查看(过去 30 天)
显示 更早的评论
I want to create a cell array 1000x11 of binary digits. I wrote the following code but when I run it, I get an array of {0×0 double} instead of {1x11}. What is wrong? How can I do that?
pop = cell(1000,11);
for i = 1:1000
pop{i}=randi([0 1],1,11)
end
0 个评论
回答(1 个)
James Tursa
2021-5-6
编辑:James Tursa
2021-5-6
Your pop variable has 1000x11 = 11000 elements. Your for-loop only assigns values to 1000 of those elements (the first column). The rest are left as NULL which evaluates to 0x0 doubles. E.g., pop{i} = etc assigns that 1x11 array to a single element of pop, not spread out to 11 elements of pop.
If you want random 0 & 1 digits I would suggest you simply use a numeric or logical array instead of a cell array. E.g.,
pop = rand(1000,11) < 0.5;
Then to get at the binary digits you just need to access the rows of pop. E.g., pop(i,:) is the i'th row which contains the 11 binary digits.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!