Error with the division

11 ビュー (過去 30 日間)
Pul
Pul 2021 年 11 月 25 日
コメント済み: Star Strider 2021 年 11 月 30 日
Hello everyone,
load('DATI_MAR_ANNUALI');
load('DATA_ECM');
a=DATIMARannuali;
b=DATIECMWFannuali;
c=(a-b)./a
d=c*100
this is my code, but I get an error trying to do the division; how can I solve it?
Thank you!
  5 件のコメント
Mathieu NOE
Mathieu NOE 2021 年 11 月 26 日
hello @Pul
can you explain what is the intention ? the tables contain multiple columns so the math you want to do is for which ones ?
Pul
Pul 2021 年 11 月 30 日
Exactly, but I solved the problem. Thank you!

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

採用された回答

Star Strider
Star Strider 2021 年 11 月 26 日
Use the table2array function to convert the contents of the table to an array. Assign the arrays to new variables rather than over-writing the tables, since the references in the tables may be useful later in the code.
However the first column of the (40x13) table ‘DATIMARannuali’ is a year value (not a datetime array) with the variable name 'Year' so including that in the calculation may not be appropriate. While I did not download and open ‘DATA_ECM.mat’ the sizes of the two tables must match for tthe element-wise calculations to succeed, and the variables must be the same (or have similar contexts) for the calculation to make any sense.
.
  2 件のコメント
Pul
Pul 2021 年 11 月 30 日
Okay, got it. I solved it.
Thank you!
Star Strider
Star Strider 2021 年 11 月 30 日
As always, my pleasure!
.

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

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2021 年 11 月 26 日
As StarStride hints, these tables are not even the same size, in either dimension:
>> DATIECMWFannuali
DATIECMWFannuali =
24×17 table
[snip]
>> DATIMARannuali
DATIMARannuali =
40×13 table
[snip]
If they were, there are a few ways to do this, all more or less syntactic variations on the same thing: a table is a container, and you need to do the math on the contents, not the container. As SS points out, one of the variables in these tables is a year number, and surely you dont want to include that in the calculation. So first, probably, convert to timetables.
Given that, the simplest thing would be something like
c = (DATIECMWFannuali.Variables-DATIMARannuali.Variables)./DATIECMWFannuali.Variables;
d = array2table(c*100,'VariableNames',DATIECMWFannuali.Properties.VariableNames);
or perhaps
a = DATIECMWFannuali.Variables; % or DATIECMWFannuali{:,:}, or table2array(DATIECMWFannuali)
b = DATIMARannuali.Variables; % or ...
c = (b-a)./a;
d = array2table(c*100,'VariableNames',DATIECMWFannuali.Properties.VariableNames);
If you leave them as tables, then probably the ...{:,:} syntax is easiest, because you can use ...{:,2:end}.
The whole topic of "doing math on data in tables" is explored here: "This example ... shows how to perform calculations by using the numeric and categorical data that the table contains." You might find that helpful.
  1 件のコメント
Pul
Pul 2021 年 11 月 30 日
Yes, got it, thank you!

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by