0

I have created cell arrays and put values using the loop.. Here is what I have done:

mycell =cell(1,100,num);       %% assume num anything
idx = 1;
[a,b] = size(depth);
for i=1:a
 for j=1:b
      if isfinite(depth(i,j)) 
      for segId=1:2
        mycell{:,idx,segId} = depth(i,j);
        idx=idx+1;

In the end i got result like this:

mycell(;,:,1) = 
[1] [1.2] [1.222] [] [] [] [0.7] [06] [] [0.34] .....[100n]

mycell(;,:,2) = 
[] [] [] [1.33] [0.5] [0.3] [] [] [0.5] [] .....[100n]

Is there any way I can delete the empty cell in these cell arrays...

Or is there any way I can get an output like this: - [1*9 values][1*88 values] and so on... like on cell array and get values in one cell...I am unable to do this because of this loop.

Matt
  • 12,848
  • 2
  • 31
  • 53
Akash
  • 103
  • 1
  • 14

1 Answers1

0

To remove empty cells, is trivial (Similar to Luis's post). Essentially this calls isempty on each cell element and then uses this logical array to index into mycell and retrieve only the non-empty values.

newcell = mycell(cellfun(@isempty, mycell));

However, you mention combining multiple elements into a single element. You can do this by using {:} expansion. What this essentially does is expand all elements that you have indexed into separate inputs. So if we make these the inputs to cat, we can concatenate all of the elements along the specified dimension.

Just to demonstrate:

a = {1, 2, 3};
cat(2, a{:});

    [1 2 3]

So in your data, we could easily combine all data along the first index of the third dimension:

item = cat(2, mycell{:,:,1});

We can also do this to completely flatten your data along the third dimension.

newcell = arrayfun(@(x)cat(2, mycell{:,:,x}), 1:size(mycell, 3), 'uni', 0);

If you take this second approach, you don't even have to worry about first removing empty cells as they will just disappear during concatenation.

a = {1, 2, 3, [], 4};
cat(2, a{:});

    [1  2  3  4]
Community
  • 1
  • 1
Suever
  • 64,497
  • 14
  • 82
  • 101
  • Thank You. "isempty" functions was not working. It removes all the empty cells but was combining the result of mycell(:,:,1) and mycell(:,:,2) into one ans, therefore I asked this question here. The last approach of your has solved the problem. – Akash Mar 22 '16 at 22:17
  • As the previous result I have achieved like [1*9 single] and so on...Is it any way I can have convert them in matrix, i.e. every single cell into different matrix. ? – Akash Mar 24 '16 at 13:30
  • @Akash Yes you can use `num2cell`. `m = num2cell(data)` – Suever Mar 24 '16 at 13:33
  • I have defined my cell as (1,100,2) so it has 1 row, 100 columns..and I am getting result as [1*9 single]. after using num2cell, I am getting values as column 1till 9 = [1][1][1]...and so on... Is it possible to get output as 3 dimensional..something like [2*2*2] or anything. I want output as xyz format so that I can treat it as an input to RANSAC – Akash Mar 24 '16 at 14:42