How to do operation between number inside two cell
1 次查看(过去 30 天)
显示 更早的评论
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
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(..............)
采纳的回答
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 个评论
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 Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!