0

I have this formula: 100-[(x-1)/(y-1)]*100, and I would like to apply it to a cell-type variable F with A rows * 14 columns in MATLAB, where x = F(:,14) and y = size(F,1).

I have been trying to get there by different attempts, this is one example I tried:

F(:,15) = cellfun(@minus, 100, ((cell2mat(F(:,14)-1)./size(F,1)) * 100));

But it is not working. Could someone help me?

Amro
  • 123,847
  • 25
  • 243
  • 454
user3557054
  • 219
  • 2
  • 11
  • Not working why? Any errors? – Marcin Jun 27 '14 at 01:18
  • `cellfun` works on **cell** arrays. By applying `cell2mat`, you are transforming a `cell` array back into a **matrix**. As such, the input into `cellfun` is no longer a `cell` array – rayryeng Jun 27 '14 at 02:20

1 Answers1

2

Assuming I understood this correctly, first let me show how to do it the way you described:

% a random cell array of 10 rows x 5 columns
% each cell contains one number
F = num2cell(rand(10,5));

% compute formula on 1st column. Result is a numeric vector
out = 100 - ((cell2mat(F(:,1))-1) ./ size(F,1))*100;

% put result back into cell array (by converting into expected format)
F(:,5) = num2cell(out);

Now since F is a cell array simply containing a scalar number in each of its cells, why don't you just use a regaular numeric matrix instead? That way you avoid calling cell2mat and num2cell back and forth...

Amro
  • 123,847
  • 25
  • 243
  • 454
  • Thank you for the help! F is a cell array but it doesn't contain only scalar numbers, it also has char. strings. My `x` ( your `F(:,1) ) `is a number though! I have been applying `cell2mat` a lot, maybe that's why I also tried it here, but I can try without too! – user3557054 Jun 27 '14 at 08:36
  • Hey! I just tried your formula, it's working perfectly. I don't know I that was your point before, but I don't need to aplly the `F = num2cell(rand(10,5));` it works! really nice :) Thanks – user3557054 Jun 27 '14 at 09:29
  • is there a way to do this without converting to mat, then to cell? – Dan Powers Nov 12 '16 at 21:55
  • @DanPowers You can write a loop over the cell elements (either implicit with `cellfun` or an explicit for-loop), however numeric operations like that are better vectorized. Ultimately I think you need to use the right kind of container; if your data/columns is all numeric is you should store it a numeric matrix, rather than a heterogeneous cell array. – Amro Nov 13 '16 at 00:15