How to do operation between number inside two cell
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ahmad Bayhaqi
am 21 Mai 2021
Kommentiert: Siddharth Bhutiya
am 24 Mai 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/625513/image.png)
Is there a way to do '-' (minus) operator between number inside cell in first column of Table A and Table B?
Thank you
2 Kommentare
Image Analyst
am 21 Mai 2021
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(..............)
Akzeptierte Antwort
Sulaymon Eshkabilov
am 22 Mai 2021
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 Kommentare
Siddharth Bhutiya
am 24 Mai 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}
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!