Search code examples
matlabmatrixformulacell-array

Apply formula on Matlab


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?


Solution

  • 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...