0

I have a 1x10 ( say 10 because it is actually 262144, but for the purpose of my question, 10 is enough) cell array called x. Each cell is an array of 0 to 4 real values.

I'd like to run an if condition based on the number of element in each cell, WITHOUT using a for loop (as I said, it is actually 262144 cell long, so a for loop might be a bit heavy).

for loop exemple to show what I want to do :

for i = 1:10
    if numel(x{i}) > 2
        h(i) = 0;
    else
        h(i) = 1;
    end
end

And then I'll want to run a while loop independantly on each cell, incrementing h by 1 until it reaches the value of the lenght of the cell.

len = cellfun(@numel,x)
while h < len
    h = h+1;
    %code that does thing on each cell still in the while loop
end

I've never seen anything like that online even though I've looked quite thoroughly. If anyone can give me a global indication on how to work independantly on separate cells without using a for loop (which is the core of my problem), I'd be great!

EDIT : Knowing how to operate on value X of all cells would be great too. E.g:

k = x{:}(1) < x{:}(2)

This doesn't work, but I don't know why and I don't what would. Trying to compare all first values with all second values.

Vissenbot
  • 227
  • 2
  • 5
  • 15

1 Answers1

1

For your first problem, you can easily use a combination of cellfun and logical indexing to generate h:

h = cellfun(@numel, x) <= 2;

The <= 2 bit will return a 1 if a value is, well, less than or equal to 2, and 0 otherwise.

Note that for and while loops really aren't slow, so maybe what you have written is "good enough".

EDIT: With regards to your new, edited question: naively this can't be done unless you can guarantee that the length of each array inside each cell is the same. I will assume that it is.

If that's the case, you can simply call cell2mat to convert the cell array to a matrix, then directly compare the two columns:

A = cell2mat(x);
k = A(:,1) < A(:,2);

If the lengths are all different, you can fill them with zeros or nan or something to make them the same length.

Community
  • 1
  • 1
Dang Khoa
  • 5,693
  • 8
  • 51
  • 80
  • As for the second bit, your while loop won't work as written since `h < len` will evaluate to a logical array, but the `while` expects a logical scalar. – Dang Khoa May 21 '14 at 14:14
  • I'll try it in a for loop and give you feedback after! – Vissenbot May 21 '14 at 14:40
  • Well, a for loop takes approximately 1 second per 250 cells so... Might not be "good enough". – Vissenbot May 21 '14 at 14:44
  • 2
    1 second per 250 cells sounds very slow. You must be doing something other than the code you've shown to take all that time. Instead of guessing what might help speed up your code, consider posting a more representative sample of code, and ask for help speeding it up. – Peter May 21 '14 at 15:21
  • It is actually a long code, the while loop being 200 lines. It wasn't relevant knowing this code. What is relevant is if I can work on different lenght cells independantly or am I stuck using a for loop. To give you an idea, I'm adapting peakfinder.m (http://www.mathworks.com/matlabcentral/fileexchange/25500-peakfinder) to work with a cell array. – Vissenbot May 21 '14 at 17:56
  • 1
    Consider using the [MATLAB Profiler](http://www.mathworks.com/help/matlab/matlab_prog/profiling-for-improving-performance.html) to find where your code can get sped up. Speeding up the loop won't help if the bottleneck is actually in the logic for the individual operations you're doing. – Dang Khoa May 21 '14 at 18:52
  • @Vissenbot if the cells are independent of each other and you have access to the Parallel Computing Toolbox, consider using a [`parfor`](http://www.mathworks.com/help/distcomp/parfor.html) loop to execute the code in parallel. – Dang Khoa May 21 '14 at 18:57
  • Yeah I'm using the MATLAB Profiler to find the bottleneck. Been at it for a couple hours. I sadly don't have access to the Parallel Computing Toolbox :( – Vissenbot May 21 '14 at 18:59