필터 지우기
필터 지우기

Vectorized generation of a cell

조회 수: 1 (최근 30일)
Amir Mahmoudi
Amir Mahmoudi 2023년 12월 22일
댓글: Matt J 2023년 12월 23일
x = 1 : a; y = 1 : b; z(x,y) = {randperm(d,2)}; a non-repeating cell is desired. No for loops please.
  댓글 수: 2
Jon
Jon 2023년 12월 22일
How are the values of a and b used. What is the value of d?
Amir Mahmoudi
Amir Mahmoudi 2023년 12월 22일
a, b, d are any numbers.

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

채택된 답변

Matt J
Matt J 2023년 12월 22일
편집: Matt J 2023년 12월 22일
There is no way to create or manipulate cells without for-loops. Things like mat2cell, num2cell, etc... all have for-loops inside them. So do things like cellfun, arrayfun, etc...
It is not clear why you would want a cell array. Your data can be stored in an a-by-b-by-2 array.
a=5; b=3; d=7;
I=nchoosek(1:d,2);
I=[I;fliplr(I)];
r=randi(height(I),a*b,1);
z= reshape(I(r,:) ,a,b,2)
z =
z(:,:,1) = 5 1 1 2 1 1 3 4 2 5 1 1 1 1 5 z(:,:,2) = 7 5 5 5 3 4 5 7 7 6 7 6 2 3 6
  댓글 수: 11
Amir Mahmoudi
Amir Mahmoudi 2023년 12월 23일
I accepted the answer because of the comments. The answer is not what I wanted.
Matt J
Matt J 2023년 12월 23일
It must be a cell. If there is no way, then I have to use for loops.
If you don't care about avoiding for-loops anymore, you can take the result of my proposed code and convert it to a cell with num2cell:
a=5; b=3; d=7;
I=nchoosek(1:d,2);
I=[I;fliplr(I)];
r=randi(height(I),a*b,1);
z= reshape( num2cell(I(r,:),2) ,a,b)
z = 5×3 cell array
{[6 2]} {[2 1]} {[5 7]} {[4 5]} {[3 5]} {[5 7]} {[1 2]} {[7 3]} {[4 7]} {[7 5]} {[3 7]} {[1 4]} {[1 4]} {[2 4]} {[2 6]}

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by