Vectorized generation of a cell

x = 1 : a; y = 1 : b; z(x,y) = {randperm(d,2)}; a non-repeating cell is desired. No for loops please.

2 Comments

How are the values of a and b used. What is the value of d?
a, b, d are any numbers.

Sign in to comment.

 Accepted Answer

Matt J
Matt J on 22 Dec 2023
Edited: Matt J on 22 Dec 2023
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 Comments

not even with cellfun?
In optimization processes (with large numbers), vectorized forms work faster than for loops.
Matt J
Matt J on 22 Dec 2023
Edited: Matt J on 22 Dec 2023
not even with cellfun?
No, cellfun is no faster than a for-loop. It is just a way to hide the for-loop inside a 1-line piece of code.
Again, though, there is no reason to be holding your result in a cell. Did you try my suggested code?
Dyuman Joshi
Dyuman Joshi on 22 Dec 2023
Edited: Dyuman Joshi on 22 Dec 2023
Like mat2cell() and num2cell() mentioned by Matt, cellfun() is a for loop in disguise as well.
A simple for loop would be a much better choice.
"In optimization processes (with large numbers), vectorized forms work faster than for loops."
Yes, if properly utilized, vectorized code is generally faster than for loops. However, that applies to numeric arrays, not cell arrays.
It will be better to work with numeric arrays, as Matt has suggested and shown above.
Thanks for your response. In fact, this is just a small part of a large coding. It must be a cell. If there is no way, then I have to use for loops.
Do you have a constraint to only use a cell array? If yes, then what is the constraint?
Thanks for useful comments.
You're welcome, but if the answer resolves your question, please Accept-click it.
r=randi(height(I),a*b,1);
Shouldn't be
r = randperm(height(I),a*b);
if you want all "cells" have different pairs?
I accepted the answer because of the comments. The answer is not what I wanted.
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]}

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 22 Dec 2023

Commented:

on 23 Dec 2023

Community Treasure Hunt

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

Start Hunting!