MATLAB Answers

How can I create n arrays of size 100 each with random integer values?

조회 수: 11(최근 30일)
Rami Kanj
Rami Kanj 13 Dec 2019
답변: Steven Lord 13 Dec 2019
c = cell(1,100)
for i = 1:100
c{i} = randi(100)
end
I tried doing this but it only gives 100 arrays of size 1.
I need 1-D arrays
Any other solution is gladly welcomed.
Thank you!

  댓글 수: 0

로그인 to comment.

채택된 답변

Alex Mcaulley
Alex Mcaulley 13 Dec 2019
c = cell(1,100)
for i = 1:100
c{i} = randi(100,1,100)
end

  댓글 수: 7

표시 이전 댓글 수: 4
Rami Kanj
Rami Kanj 13 Dec 2019
Not quite I need 100 1D arrays each containing 100 elements.
Without cell it is a hassle but I think a way around this without using cell is to create 1 big 1D array which contains 10,000 elements and take 100 elements each time and place it into an array and add an offset to take the next 100 elements until it ends.
Alex Mcaulley
Alex Mcaulley 13 Dec 2019
It is simpler with @Guillaume suggestion, creating 100x100 matrix and then, each row (or each column) is an array of 100 elements:
result = randi(50, 100)
result(1,:) %1x100 array

로그인 to comment.

추가 답변(2개)

Bhaskar R
Bhaskar R 13 Dec 2019
c = cell(1,100);
for i = 1:100
c{i} = randperm(100);
end

  댓글 수: 0

로그인 to comment.


Steven Lord
Steven Lord 13 Dec 2019
Since all of your arrays are the same size, I would consider stacking them in one of the dimensions in which they have size 1.
A = randi([-10 10], 5, 8);
In this example A contains 5 "arrays" (the rows) of random integer values between -10 and 10, each "array" having size [1 8]. To use the third "array":
third = A(3, :)
For 2-dimensional arrays, you could stack them in the third dimension like pages in a book.
B = randi([-10 10], 3, 3, 7);
fifth = B(:, :, 5)

  댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by