필터 지우기
필터 지우기

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
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)
Columns 1 through 18 {0×0 double} {[0]} {[1]} {[1]} {[0]} {[0]} {0×0 double} {[0]} {[1]} {0×0 double} {[1]} {[1]} {0×0 double} {[0]} {[0]} {[1]} {0×0 double} {[1]} Columns 19 through 20 {[0]} {[0]}
Bruno Luong
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
A = 1×20
NaN 1 0 0 0 1 0 NaN 0 1 1 1 NaN 0 0 1 0 0 1 0

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
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.

추가 답변 (1개)

Walter Roberson
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
out = 1×25 cell array
Columns 1 through 16 {[1]} {[1]} {[0]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {[0]} {[0]} {[1]} {[0]} {[0]} {[0]} {[0]} {0×0 double} {0×0 double} Columns 17 through 25 {0×0 double} {0×0 double} {[1]} {[0]} {[0]} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
  댓글 수: 1
Yasir
Yasir 2023년 9월 20일
Thank you very much for your replies.
Actualy I want to genereate pseudorandom integers zeros and onces from two souces. The first source is the owner, in case the first souce didn't send bits, the second sources will start sending. This is why I was thinking to generate numeric array using randi with empty cells (four consecutive empty cells), where these empty cell will be filled later from second source.
All gernerated bit stream will be then used as input to 16QAM modulator.
Thanks and regards

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by