How to do operation between number inside two cell

1 回表示 (過去 30 日間)
Ahmad Bayhaqi
Ahmad Bayhaqi 2021 年 5 月 21 日
コメント済み: Siddharth Bhutiya 2021 年 5 月 24 日
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 件のコメント
Image Analyst
Image Analyst 2021 年 5 月 21 日
Make it easy for us to help you. Please attach the table in a .mat file. I think you'll need to extract the cell array from the table first, then extract the contents of the cell arrays after that. Then do your math. Something like (untested) because you did not attach your data
ca1 = t{1, 1};
ca2 = t{2, 1};
if length(ca1) == length(ca2)
% Then do something like subtract them
contents1 = cell2mat(..............)
Ahmad Bayhaqi
Ahmad Bayhaqi 2021 年 5 月 22 日
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.
your help is greatly appreciated.
Thank you

サインインしてコメントする。

採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 5 月 22 日
Hi,
Here is the simple solution to your exercise:
clc
D1 = load('climatology.mat');
D2=load('daily.mat');
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.
  3 件のコメント
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 5 月 24 日
Most welcome! It is just a pleasure!
Siddharth Bhutiya
Siddharth Bhutiya 2021 年 5 月 24 日
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}

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by