Error with the division
Ältere Kommentare anzeigen
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 Kommentare
Steven Lord
am 25 Nov. 2021
What is the full and exact text (everything displayed in red and/or orange in the Command Window) of the error and/or warning messages you receive when you run your code? The exact text of the messages may include information that will help us determine the problem or at least the next step to take in investigating the problem.
Mathieu NOE
am 25 Nov. 2021
hello (again !)
a and b are tables - you cannot do directly
c=(a-b)./a
d=c*100
you can do it f you select elements from the tables
Pul
am 25 Nov. 2021
Mathieu NOE
am 26 Nov. 2021
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
am 30 Nov. 2021
Akzeptierte Antwort
Weitere Antworten (1)
Peter Perkins
am 26 Nov. 2021
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 Kommentar
Pul
am 30 Nov. 2021
Kategorien
Mehr zu Tables finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!