I want to gererate pseudorandom integers using randi function, however I want have empty cells between the integers.
1 次查看(过去 30 天)
显示 更早的评论
I want to gererate pseudorandom integers using randi function, however I want have empty cells between the integers, for example
1001011100 [] [] [] []100011[] [] [] []
Thank you in advance
2 个评论
Dyuman Joshi
2023-9-19
It's not possible to have empty "cells" (elements) in a numeric array.
There are workarounds possible involving cell arrays -
%Total length
N = 20;
y = num2cell(randi(2,1,N)-1);
%Amount of empty doubles to have in the cell array
n = 5;
k = randperm(N,n);
y(k) = {[]};
disp(y)
Bruno Luong
2023-9-19
编辑:Bruno Luong
2023-9-19
All those comments/answers about numerical array cannot have holes but I'm surprised nobody suggests to put instead NaN (aka double.missing) at the place where OP want to put empty array?
A = randi([0 1],1,20);
A(randi(end,1,4)) = NaN;
A
采纳的回答
Steven Lord
2023-9-19
Numeric arrays in MATLAB can't have "holes".
Perhaps if you describe in more detail how you're hoping to use this type of array we may be able to suggest a data storage strategy that will facilitate your use case. It's possible, for example, that a sparse array may be what you're looking for.
0 个评论
更多回答(1 个)
Walter Roberson
2023-9-19
It is not possible to have empty positions in a numeric array. You would need to use a cell array, such as
N = 3;
out = {};
for K = 1 : N
thislen = randi(10);
thispart = num2cell(randi([0 1], 1, thislen));
out = [out, thispart, {[] [] [] []}];
end
out
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!