user2040072
user2040072

Reputation: 15

multiple cell array looping

I am wondering folks how can I loop over two different data cell arrays.

More precisely, The first

data1 = {'x','y','z', 'xyz','yxz'};
data2 = {'b','c','a'};

I want a for loop that performs the following operation

iterates on the first element of data2 while iterating over the entire elements of data1

Hope you guys can understand my question and looking forward to your amazing talent

Thank you

Upvotes: 0

Views: 108

Answers (2)

Shai
Shai

Reputation: 114786

You can use nested cellfun depending on what you want to do with data2{ii} and data1{jj}...

res = cellfun( @( d1 ) cellfun( @( d2 ) myfun( d1, d2 ), data2, 'uni', 0 ), data1, 'uni', 0 );

Upvotes: 2

p8me
p8me

Reputation: 1860

data1 = {'x','y','z', 'xyz','yxz'};
data2 = {'b','c','a'};

for k = data2
    for m = data1
        [k{1} m{1}] % Print or use them
    end
end

where k and m are 1x1 cell arrays, and you can access the string inside them with k{1} or k{:} (all the elements which is only 1 now).

Upvotes: 1

Related Questions