How to do operation between number inside two cell
    조회 수: 7 (최근 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에서 Matrix Indexing에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!