Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
cellfun doesn't work ?

Subject: cellfun doesn't work ?

From: Abel

Date: 25 Jun, 2013 07:29:27

Message: 1 of 6

Hi,

I'm trying this simple code :

clear
X = [1 3
10 12
17 24
29 35
41 46
51 58
60 65
73 79
82 84
91 99
102 104
112 116
118 123];

Y = cellfun(@(Y) Y(1):Y(2), num2cell(X,2),'UniformOutput', false).';
Y = cell2mat(Y).';

But it doesn't work, I get this error :

??? Too many inputs.

It looks like it is due to cellfun. How could I correct it ?

Subject: cellfun doesn't work ?

From: Abel

Date: 25 Jun, 2013 09:40:26

Message: 2 of 6

It seems this code can't work with my version of Matlab (7.0.1.24704 (R14) Service Pack 1 ) although it is syntactically correct. But, would you know an alternative method ?

What I want to do is the following :

Let's say,

X = [1 3
10 12
17 24
29 35
41 46
51 58
60 65
73 79
82 84
91 99
102 104
112 116
118 123];

(X has then 2 columns)

I want to obtain :

Y=[1 2 3 10 11 12 17 18 19 20 21 22 23 24 ect...]

(Y has one column)

It is what is supposed the previous code to do. Moreover, I have managed to do what I want with a code than includes one for loop but I would like to avoid this. That is why I'm asking for a solution without any loops.

Subject: cellfun doesn't work ?

From: james bejon

Date: 25 Jun, 2013 16:17:07

Message: 3 of 6

% Data
X = [1 3; 10 12; 17 24; 112 116];

% One way
cell2mat( arrayfun(@(i) X(i, 1):X(i, 2), 1:length(X), 'UniformOutput', 0) )


% Another (not nice)
eval(['[', sprintf('%d:%d,', X.'), ']'])


% There are probably file exchange submissions that do the same thing much more elegantly
% I think there might be a submission called "splitVec" that can be used to do it.

Subject: cellfun doesn't work ?

From: Bruno Luong

Date: 25 Jun, 2013 18:33:24

Message: 4 of 6

"james bejon" wrote in message <kqcfq2$542$1@newscl01ah.mathworks.com>...

> % There are probably file exchange submissions that do the same thing much more elegantly

Yes:
http://www.mathworks.com/matlabcentral/fileexchange/29854

>> mcolon(X(:,1),X(:,2))

ans =

  Columns 1 through 13

     1 2 3 10 11 12 17 18 19 20 21 22 23

  Columns 14 through 26

    24 29 30 31 32 33 34 35 41 42 43 44 45

  Columns 27 through 39

    46 51 52 53 54 55 56 57 58 60 61 62 63

  Columns 40 through 52

    64 65 73 74 75 76 77 78 79 82 83 84 91

  Columns 53 through 65

    92 93 94 95 96 97 98 99 102 103 104 112 113

  Columns 66 through 74

   114 115 116 118 119 120 121 122 123

% Bruno

Subject: cellfun doesn't work ?

From: Abel

Date: 27 Jun, 2013 06:56:07

Message: 5 of 6

Thank you Bruno, unfortunately it doesn't seem to work :

I've tried : Y=mcolon(X(:,1),X(:,2)); with my previous example and I get this error message :

??? At compilation "mcolsettol" was determined to be variable. As a variable,
it is uninitialized, but function "mcolsettol" is now in context.
In MATLAB 7, you cannot use the same name in a single function as
both a function and a variable.

Error in ==> mcolon at 24
tol = mcolsettol();

Despite of this, is there something to do regarding my matlab version (7.0.1.24704 (R14) Service Pack 1) ?


------------------------------

The other solution doesn't work either, "arrayfun" being not recognized :

>> Y=cell2mat( arrayfun(@(i) X(i, 1):X(i, 2), 1:length(X), 'UniformOutput', 0) );
??? Undefined command/function 'arrayfun'.

Subject: cellfun doesn't work ?

From: Jan Simon

Date: 28 Jun, 2013 10:21:07

Message: 6 of 6

Hi Abel,

Are the intervals pairwise distinct and sorted?
Then this could help:

  index = zeros(1, X(end, 2) + 1);
  index(X(:, 1)) = 1;
  index(X(:, 2) + 1) = -1;
  Result = find(cumsum(index));
 
[not tested]
Of course this will be cruel for e.g. [1,2; 1e12,1e12+1].
Then a simple FOR loop would work:

  C = cell(1, size(X, 1));
  for k = 1:size(X, 1)
    C{k} = X(k, 1):X(k, 2);
  end
  Result = cat(2, C{:});

The last line is performed much faster by:
  http://www.mathworks.com/matlabcentral/fileexchange/28916-cell2vec
It seems like Matlab's CAT has problems with the pre-allocation.
But of course Bruno's mcolon is the most efficient solution, when you can get it to run on your machine.

Kind regards, Jan

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us