# How to do operation between number inside two cell

1 view (last 30 days)
Ahmad Bayhaqi on 21 May 2021
Commented: Siddharth Bhutiya on 24 May 2021
Hi All,
I have two tables, table A and B. Each table consists of three coloumns defining the longitude, latitude and temperature value. The temperature coloum is in cell type.
Then, each of row of cell coloumn consists of different number. Please see the picture. Is there a way to do '-' (minus) operator between number inside cell in first column of Table A and Table B?
Thank you
##### 2 CommentsShowHide 1 older comment
Ahmad Bayhaqi on 22 May 2021
Thank you very much for your response.
Herewith , I attach the mat file of my data. I want to do "daily minus climatolog" for the first coloumn.
However, I havent got the result yet.
Thank you

Sulaymon Eshkabilov on 22 May 2021
Hi,
Here is the simple solution to your exercise:
clc
LAT = D1.nu_table4.Lat-D2.new_table4.Lat; % You can also perform '+'
LON = D1.nu_table4.Lon-D2.new_table4.Lon; % You can also perform '+'
for jj=1:5
T1{1, jj} = D1.nu_table4.extdata{ii,1}{jj,1}-D2.new_table4.extData{ii,1}{jj,1}; % You can also perform '+'
end
for jj=1:4
T2{1, jj} = D1.nu_table4.extdata{ii,1}{jj,1}-D2.new_table4.extData{ii,1}{jj,1}; % U may use'+'
end
for jj=1:4
T3{1, jj} = D1.nu_table4.extdata{ii,1}{jj,1}-D2.new_table4.extData{ii,1}{jj,1};
end
for jj=1:5
T4{1, jj} = D1.nu_table4.extdata{ii,1}{jj,1}-D2.new_table4.extData{ii,1}{jj,1};
end
for jj=1:5
T5{1, jj} = D1.nu_table4.extdata{ii,1}{jj,1}+D2.new_table4.extData{ii,1}{jj,1};
end
Good luck.
Siddharth Bhutiya on 24 May 2021
You could generalize this approach by using cellfun. First of all you can write a small function that would handle the subtraction for each row. So it will iterate over each double vector inside the cell array and subtract them. Then you can simply use cellfun with this function to deal with the entire table.
function out = cell_subtract(a,b)
assert(isequal(size(a),size(b))); % Make sure both have the same sizes
out = cell(size(a));
for i = 1:numel(out)
assert(isequal(size(a{i}),size(b{i}))); % Internal sizes should match
% a{i} and b{i} are double vectors so they can be directly subtracted.
out{i} = a{i} - b{i};
end
end
>> a
a =
5×3 table
extdata Lat Lon
__________ _______ ______
{5×1 cell} -12.875 90.625
{4×1 cell} -12.875 90.875
{4×1 cell} -12.875 91.125
{5×1 cell} -12.875 91.375
{4×1 cell} -12.875 91.625
>> b
b =
5×3 table
extData Lat Lon
__________ _______ ______
{5×1 cell} -12.875 90.625
{4×1 cell} -12.875 90.875
{4×1 cell} -12.875 91.125
{5×1 cell} -12.875 91.375
{4×1 cell} -12.875 91.625
>> cellfun(@cell_subtract,a.extdata,b.extData,'UniformOutput',false)
ans =
5×1 cell array
{5×1 cell}
{4×1 cell}
{4×1 cell}
{5×1 cell}
{4×1 cell}