Generate cell of random numbers with same size arrays

조회 수: 7 (최근 30일)
Riccardo Zabatta
Riccardo Zabatta 2021년 3월 6일
댓글: Riccardo Zabatta 2021년 3월 8일
Hello!
I'd like to generate a 3x2 cell, whose arrays are all matrices 5x100 of random numbers.
I am a bit struggling with it, despite I think is quite easy to do.
Any suggestion?
Thanks
  댓글 수: 3
Riccardo Zabatta
Riccardo Zabatta 2021년 3월 6일
What I've tried so far is something derived I found on the community.
%lowercase variables are MATRICES, capitals are CELLS
%define a, matrix 18x100 of random integers
a = randi(50,18,100);
%define A, cell derived from a, where its 18 rows are splitted in three 6-rows arrays
A = mat2cell(a,[6,6,6],100);
So, this way A results to be a 3x1 cell.
But I need it to be 3x2, so I tried to define B:
%ATTEMPT 1)
%define B, cell derived from matrix a, where its "structure" is splited in six 18x50 arrays
B = mat2cell(a,([6,6,6],50),([6,6,6],50));
But this does not work bc of syntax
I solved my problem by defining, same as I did for A, a new 3x1 cell called C and then I defined my definitive cell (let's call it D):
%ATTEMPT 2)
%define a,b matrices 18x100 of random integers
a = randi(50,18,100);
b = randi(50,18,100);
%define A,B cells derived from a,b respectively.
% Where their 18 rows are splitted in three 6-rows arrays
A = mat2cell(a,[6,6,6],100); % 3x1 cell
B = mat2cell(b,[6,6,6],100); % 3x1 cell
E = [A,B];
But still I am wondering if there is a way to get success from attempt 1)
Jan
Jan 2021년 3월 6일
A a 3x2 cell with containing 5x100 matrices, a 15 x 200 random matrix is needed. Why do you create a 18x100 matrix?

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

채택된 답변

Jan
Jan 2021년 3월 6일
편집: Jan 2021년 3월 6일
Creating a large array only to split it into parts needs more RAM than creating the random matrices in parts directly:
C = cell(3, 2);
for k = 1:numel(C)
C{k} = randi(50, 5, 100);
end
If mat2cell is required for any reasons:
a = randi(50, 15, 200);
A = mat2cell(a, [5, 5, 5], [100, 100])

추가 답변 (1개)

John D'Errico
John D'Errico 2021년 3월 6일
편집: John D'Errico 2021년 3월 6일
Simple enough.
A = rand(15,200);
B = mat2cell(A,repmat(5,3,1),repmat(100,1,2));
size(B)
ans = 1×2
3 2
size(B{1,1})
ans = 1×2
5 100
class(B{1,1})
ans = 'double'
So B is a cell array of size 3x2.
Each element of B is a regular double precision array, of size 5x100. You can put whatever you kind of random numbers you wish into the original array A.

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by