how to generate cell array based on the size of it.
조회 수: 2 (최근 30일)
이전 댓글 표시
My command is B = arrayfun(@(N) randi(2,1,N), repelem((1:3).',5), 'uniform', 0)
when i run it generates B= 15×1 cell array
{[ 2]}
{[ 1]}
{[ 3]}
{[ 3]}
{[ 2]}
{1×2 double}
{1×2 double}
{1×2 double}
{1×2 double}
{1×2 double}
{1×3 double}
{1×3 double}
{1×3 double}
{1×3 double}
{1×3 double}
but the values inside the array are
1
1
2
1
2
[2,1]
[2,1]
[1,2]
[2,2]
[1,1]
[2,2,1]
[1,2,1]
[2,2,2]
[1,1,1]
[2,1,2]
Actually i need to get
1
1
3
1
1
for first 5 rows the value should be 1 as it is 1x1 double
[1,2]
[2,1]
[3,2]
[3,2]
[2,1]
for the next 5 rows the values should be 1,2 and not more than 2, and should not have 2 by leaving 1
[1,2,1]
[3,1,3]
[2,3,3]
[1,2,2]
[3,3,1]
for the next 5 rows the values should not have 3 by leaving 2 and should not have 2 by leaving 1.
Could anyone please help me with this.
댓글 수: 0
채택된 답변
Mohammad Sami
2021년 6월 24일
I am bit confused what you mean by should not have 3 by leaving 2 and should not have 2 by leaving 1.
I assume you just want the numbers in order 1,2,3.
B = arrayfun(@(N) colon(1,N), repelem((1:3).',5), 'UniformOutput', false)
댓글 수: 11
Mohammad Sami
2021년 7월 6일
You can simply generate A or another variable. Then you can use randperm to sample A to get B. For example if you have an array of 20 values and you want to choose 10, you can do like this.
if true
A = 1:2:40;
B = A(randperm(20,10));
end
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!