Finding the maximal value in a matrix, within a specific column, and show the value in the output.

1 Ansicht (letzte 30 Tage)
Hello, I have a matrix that I need to extract the maximal value of column D, which is the 4th column.
I use the following command
D=readtable('tempDataTrollhFlygpl.xlsx')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
D = 6789×7 table
Tidsperiod_fr_o_m_ Tidsperiod_t_o_m_ H_jd_meter_verHavet_ Latitud_decimalgrader_ Longitud_decimalgrader_ Var6 Var7 ____________________ ____________________ ____________________ ______________________ _______________________ ____ _____________________________________________________________________________________________________________________________________________ 01-Jan-1961 00:00:00 01-Aug-1979 23:59:59 NaT {'58.3141' } {'12.3292' } NaN {0×0 char } NaT NaT NaT {0×0 char } {0×0 char } NaN {0×0 char } NaT NaT NaT {'Lufttemperatur'} {'Kvalitet'} NaN {'Tidsutsnitt:' } 01-Jan-1961 00:00:01 02-Jan-1961 00:00:00 01-Jan-1961 {'0.1' } {'Y' } NaN {'Kvalitetskontrollerade historiska data (utom de senaste 3 mån)' } 02-Jan-1961 00:00:01 03-Jan-1961 00:00:00 02-Jan-1961 {'0.7' } {'Y' } NaN {'Tidsperiod (fr.o.m.) = 1961-01-01 00:00:00 (UTC)' } 03-Jan-1961 00:00:01 04-Jan-1961 00:00:00 03-Jan-1961 {'1.0' } {'Y' } NaN {'Tidsperiod (t.o.m.) = 1979-08-01 23:59:59 (UTC)' } 04-Jan-1961 00:00:01 05-Jan-1961 00:00:00 04-Jan-1961 {'0.4' } {'Y' } NaN {'Medelvärdet är beräknat för = 24 timmar' } 05-Jan-1961 00:00:01 06-Jan-1961 00:00:00 05-Jan-1961 {'0.0' } {'Y' } NaN {0×0 char } 06-Jan-1961 00:00:01 07-Jan-1961 00:00:00 06-Jan-1961 {'0.2' } {'Y' } NaN {'Kvalitetskoderna:' } 07-Jan-1961 00:00:01 08-Jan-1961 00:00:00 07-Jan-1961 {'0.0' } {'Y' } NaN {'Grön (G) = Kontrollerade och godkända värden.' } 08-Jan-1961 00:00:01 09-Jan-1961 00:00:00 08-Jan-1961 {'-1.1' } {'Y' } NaN {'Gul (Y) = Misstänkta eller aggregerade värden. Grovt kontrollerade arkivdata och okontrollerade realtidsdata (senaste 2 tim).' } 09-Jan-1961 00:00:01 10-Jan-1961 00:00:00 09-Jan-1961 {'-1.1' } {'Y' } NaN {0×0 char } 10-Jan-1961 00:00:01 11-Jan-1961 00:00:00 10-Jan-1961 {'-0.8' } {'Y' } NaN {'Orsaker till saknade data:' } 11-Jan-1961 00:00:01 12-Jan-1961 00:00:00 11-Jan-1961 {'-5.4' } {'Y' } NaN {'stationen eller givaren har varit ur funktion.' } 12-Jan-1961 00:00:01 13-Jan-1961 00:00:00 12-Jan-1961 {'-6.8' } {'Y' } NaN {'kvalitetskontrollerna har felmarkerat data (röd kvalitetskod). Efterföljande manuell granskning godkänner, rättar eller underkänner data.'} 13-Jan-1961 00:00:01 14-Jan-1961 00:00:00 13-Jan-1961 {'2.6' } {'Y' } NaN {0×0 char }
[~,x] = max(D(:,4));
Error using tabular/max
Applying the function 'max' to the variable 'Latitud_decimalgrader_' generated an error.

Caused by:
Error using max
Invalid data type. First argument must be numeric or logical.
B = D(x,:);
However, it does not work, and I get the error
Error using tabular/max
Applying the function 'max' to the variable 'Latitud_decimalgrader_' generated an error.
Error in untitled (line 2)
[~,x] = max(D(:,4));
Caused by:
Error using max
Invalid data type. First argument must be numeric or logical.
I should specify the rows after row 11 I think, but how that done?
I tried islocalmax instead, with plot command, but also this case didn't work.
A=readtable('tempDataTrollhFlygpl.xlsx')
plot(A(:,3:end), A(:,4:end))
xlabel('Temperature')
ylabel('Time')
TF1 = islocalmax(A,'FlatSelection','first');
Thanks

Akzeptierte Antwort

Voss
Voss am 14 Feb. 2024
Bearbeitet: Voss am 14 Feb. 2024
D(:,4) is a table (consisting of one column - the 4th column of D), not a numeric array; that's what the error is trying to tell you.
To get the data out of the table, you can use the syntax D{:,4}. However, in this case, column 4 of your table is a cell array of character vectors, not a numeric array, so you'll get a similar error using that syntax. (Note the single quotes around the numbers in column 4 below, indicating character vectors.)
D=readtable('tempDataTrollhFlygpl.xlsx')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
D = 6789×7 table
Tidsperiod_fr_o_m_ Tidsperiod_t_o_m_ H_jd_meter_verHavet_ Latitud_decimalgrader_ Longitud_decimalgrader_ Var6 Var7 ____________________ ____________________ ____________________ ______________________ _______________________ ____ _____________________________________________________________________________________________________________________________________________ 01-Jan-1961 00:00:00 01-Aug-1979 23:59:59 NaT {'58.3141' } {'12.3292' } NaN {0×0 char } NaT NaT NaT {0×0 char } {0×0 char } NaN {0×0 char } NaT NaT NaT {'Lufttemperatur'} {'Kvalitet'} NaN {'Tidsutsnitt:' } 01-Jan-1961 00:00:01 02-Jan-1961 00:00:00 01-Jan-1961 {'0.1' } {'Y' } NaN {'Kvalitetskontrollerade historiska data (utom de senaste 3 mån)' } 02-Jan-1961 00:00:01 03-Jan-1961 00:00:00 02-Jan-1961 {'0.7' } {'Y' } NaN {'Tidsperiod (fr.o.m.) = 1961-01-01 00:00:00 (UTC)' } 03-Jan-1961 00:00:01 04-Jan-1961 00:00:00 03-Jan-1961 {'1.0' } {'Y' } NaN {'Tidsperiod (t.o.m.) = 1979-08-01 23:59:59 (UTC)' } 04-Jan-1961 00:00:01 05-Jan-1961 00:00:00 04-Jan-1961 {'0.4' } {'Y' } NaN {'Medelvärdet är beräknat för = 24 timmar' } 05-Jan-1961 00:00:01 06-Jan-1961 00:00:00 05-Jan-1961 {'0.0' } {'Y' } NaN {0×0 char } 06-Jan-1961 00:00:01 07-Jan-1961 00:00:00 06-Jan-1961 {'0.2' } {'Y' } NaN {'Kvalitetskoderna:' } 07-Jan-1961 00:00:01 08-Jan-1961 00:00:00 07-Jan-1961 {'0.0' } {'Y' } NaN {'Grön (G) = Kontrollerade och godkända värden.' } 08-Jan-1961 00:00:01 09-Jan-1961 00:00:00 08-Jan-1961 {'-1.1' } {'Y' } NaN {'Gul (Y) = Misstänkta eller aggregerade värden. Grovt kontrollerade arkivdata och okontrollerade realtidsdata (senaste 2 tim).' } 09-Jan-1961 00:00:01 10-Jan-1961 00:00:00 09-Jan-1961 {'-1.1' } {'Y' } NaN {0×0 char } 10-Jan-1961 00:00:01 11-Jan-1961 00:00:00 10-Jan-1961 {'-0.8' } {'Y' } NaN {'Orsaker till saknade data:' } 11-Jan-1961 00:00:01 12-Jan-1961 00:00:00 11-Jan-1961 {'-5.4' } {'Y' } NaN {'stationen eller givaren har varit ur funktion.' } 12-Jan-1961 00:00:01 13-Jan-1961 00:00:00 12-Jan-1961 {'-6.8' } {'Y' } NaN {'kvalitetskontrollerna har felmarkerat data (röd kvalitetskod). Efterföljande manuell granskning godkänner, rättar eller underkänner data.'} 13-Jan-1961 00:00:01 14-Jan-1961 00:00:00 13-Jan-1961 {'2.6' } {'Y' } NaN {0×0 char }
"I should specify the rows after row 11 I think, but how that done?"
Specify 'NumHeaderLines', 10 in your readtable call:
D=readtable('tempDataTrollhFlygpl.xlsx', 'NumHeaderLines', 10) % specifying 10 header lines
D = 6786×7 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 ____________________ ___________ ___________ ________ _____ ____ _____________________________________________________________________________________________________________________________________________ 01-Jan-1961 00:00:01 02-Jan-1961 01-Jan-1961 {'0.1' } {'Y'} NaN {'Kvalitetskontrollerade historiska data (utom de senaste 3 mån)' } 02-Jan-1961 00:00:01 03-Jan-1961 02-Jan-1961 {'0.7' } {'Y'} NaN {'Tidsperiod (fr.o.m.) = 1961-01-01 00:00:00 (UTC)' } 03-Jan-1961 00:00:01 04-Jan-1961 03-Jan-1961 {'1.0' } {'Y'} NaN {'Tidsperiod (t.o.m.) = 1979-08-01 23:59:59 (UTC)' } 04-Jan-1961 00:00:01 05-Jan-1961 04-Jan-1961 {'0.4' } {'Y'} NaN {'Medelvärdet är beräknat för = 24 timmar' } 05-Jan-1961 00:00:01 06-Jan-1961 05-Jan-1961 {'0.0' } {'Y'} NaN {0×0 char } 06-Jan-1961 00:00:01 07-Jan-1961 06-Jan-1961 {'0.2' } {'Y'} NaN {'Kvalitetskoderna:' } 07-Jan-1961 00:00:01 08-Jan-1961 07-Jan-1961 {'0.0' } {'Y'} NaN {'Grön (G) = Kontrollerade och godkända värden.' } 08-Jan-1961 00:00:01 09-Jan-1961 08-Jan-1961 {'-1.1'} {'Y'} NaN {'Gul (Y) = Misstänkta eller aggregerade värden. Grovt kontrollerade arkivdata och okontrollerade realtidsdata (senaste 2 tim).' } 09-Jan-1961 00:00:01 10-Jan-1961 09-Jan-1961 {'-1.1'} {'Y'} NaN {0×0 char } 10-Jan-1961 00:00:01 11-Jan-1961 10-Jan-1961 {'-0.8'} {'Y'} NaN {'Orsaker till saknade data:' } 11-Jan-1961 00:00:01 12-Jan-1961 11-Jan-1961 {'-5.4'} {'Y'} NaN {'stationen eller givaren har varit ur funktion.' } 12-Jan-1961 00:00:01 13-Jan-1961 12-Jan-1961 {'-6.8'} {'Y'} NaN {'kvalitetskontrollerna har felmarkerat data (röd kvalitetskod). Efterföljande manuell granskning godkänner, rättar eller underkänner data.'} 13-Jan-1961 00:00:01 14-Jan-1961 13-Jan-1961 {'2.6' } {'Y'} NaN {0×0 char } 14-Jan-1961 00:00:01 15-Jan-1961 14-Jan-1961 {'0.3' } {'Y'} NaN {0×0 char } 15-Jan-1961 00:00:01 16-Jan-1961 15-Jan-1961 {'2.5' } {'Y'} NaN {0×0 char } 16-Jan-1961 00:00:01 17-Jan-1961 16-Jan-1961 {'-2.7'} {'Y'} NaN {0×0 char }
Now, you need to convert those character vectors to numbers, either by setting the appropriate import options before readtable is called, or after the fact by converting e.g. using str2double
val = str2double(D{:,4})
val = 6786×1
0.1000 0.7000 1.0000 0.4000 0 0.2000 0 -1.1000 -1.1000 -0.8000
[~,x] = max(val)
x = 5333
B = D(x,:)
B = 1×7 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 ____________________ ___________ ___________ ________ _____ ____ __________ 08-Aug-1975 00:00:01 09-Aug-1975 08-Aug-1975 {'25.3'} {'Y'} NaN {0×0 char}

Weitere Antworten (0)

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by