How to fill cell array with random numbers without for loop?

6 views (last 30 days)
I have created a cell array:
startSeedsX=cell(1,20);
Now I want each cell to have random number in the range 0 - 100:
startSeedsX=cellfun(@rand*100,startSeedsX);
Brings me error: Undefined operator '*' for input arguments of type 'function_handle'
I tried to put rand*100 in brackets (rand*100) or leave rand alone, doesn't help.
Is there a way to avoid for loop in that case?

Accepted Answer

Jos (10584)
Jos (10584) on 25 Feb 2018
Your definition of the function is incorrect. To fill a cell array with random numbers:
startSeedsX=arrayfun@(@() rand*100, 1:20, 'un', 0)
However, since each cell holds a single value, a regular array would be much more convenient:
startSeedsX = rand(1,20)*100

More Answers (2)

John D'Errico
John D'Errico on 25 Feb 2018
Sure. No loops are needed. You can do this in one line if you want, although I often suggest splitting long lines up into smaller operations, since that makes it easier to understand what and why you are doing something when you need to debug that code a year later.
First, create a REGULAR array filled with random numbers, without a loop. You know how to do that, right? If not, then read the help for rand. Which tells you how to do exactly that.
Once you have this regular random array of size [1,20], then do some quick reading about mat2cell.
The idea is that mat2cell can convert the array into a cell array of the same shape, but where each cell has now one element from the original array.
In the end though, while loops make your code look clumsy, they are not always bad things when you don't know any better way to solve the problem. Only if the loop makes your code inefficient should you really worry about replacing them. Once your coding skills improve, you will automatically code most things without any loops at all.

Image Analyst
Image Analyst on 25 Feb 2018
The brute force way is one way to avoid a for loop:
startSeedsX=cell(1,20);
startSeedsX{1} = 100*rand;
startSeedsX{2} = 100*rand;
startSeedsX{3} = 100*rand;
startSeedsX{4} = 100*rand;
startSeedsX{5} = 100*rand;
startSeedsX{6} = 100*rand;
startSeedsX{7} = 100*rand;
startSeedsX{8} = 100*rand;
startSeedsX{9} = 100*rand;
startSeedsX{10} = 100*rand;
startSeedsX{11} = 100*rand;
startSeedsX{12} = 100*rand;
startSeedsX{13} = 100*rand;
startSeedsX{14} = 100*rand;
startSeedsX{15} = 100*rand;
startSeedsX{16} = 100*rand;
startSeedsX{17} = 100*rand;
startSeedsX{18} = 100*rand;
startSeedsX{19} = 100*rand;
startSeedsX{20} = 100*rand;
celldisp(startSeedsX);
Not sure why you'd prefer this over a simple for loop though. You DO realize that a for loop for only 20 elements will be blindingly fast don't you? If you really want to save time, avoid the cell array and use a simple numerical array. Cell arrays take up a tremendous amount of memory/overhead.
  2 Comments
Image Analyst
Image Analyst on 25 Feb 2018
But like Jos and I both recommended, the double array realization is much better than using the cell array. Oh well, we can't force you to to it the better way, so good luck.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!