How to search for Max and Min value?
3 views (last 30 days)
Show older comments
Hi all,
I would to know how to write script(s) that extract the maximum and minimum value for each column in table below? and how to display the correspond 'dn' for the max/min value? I believe there was 2 max value and 1 min value. Thanks in advance
dn L(-20) L(-10) L(0) L(10) L(20)
-------------------------------------------
10 13.130 12.546 12.000 11.454 10.870
20 13.034 12.500 12.000 11.500 10.966
30 12.908 12.439 12.000 11.561 11.092
40 12.757 12.366 12.000 11.634 11.243
50 12.588 12.285 12.000 11.715 11.412
60 12.406 12.196 12.000 11.804 11.594
70 12.215 12.104 12.000 11.896 11.785
80 12.020 12.009 12.000 11.991 11.980
90 11.824 11.915 12.000 12.085 12.176
100 11.632 11.822 12.000 12.178 12.368
110 11.448 11.733 12.000 12.267 12.552
120 11.275 11.649 12.000 12.351 12.725
130 11.120 11.575 12.000 12.425 12.880
140 10.989 11.511 12.000 12.489 13.011
150 10.887 11.462 12.000 12.538 13.113
160 10.819 11.430 12.000 12.570 13.181
170 10.790 11.416 12.000 12.584 13.210
180 10.801 11.421 12.000 12.579 13.199
190 10.852 11.445 12.000 12.555 13.148
200 10.939 11.487 12.000 12.513 13.061
210 11.058 11.545 12.000 12.455 12.942
220 11.203 11.614 12.000 12.386 12.797
230 11.368 11.694 12.000 12.306 12.632
240 11.548 11.781 12.000 12.219 12.452
250 11.737 11.873 12.000 12.127 12.263
260 11.931 11.967 12.000 12.033 12.069
270 12.127 12.062 12.000 11.938 11.873
280 12.320 12.155 12.000 11.845 11.680
290 12.507 12.246 12.000 11.754 11.493
300 12.683 12.331 12.000 11.669 11.317
310 12.843 12.408 12.000 11.592 11.157
320 12.981 12.474 12.000 11.526 11.019
330 13.091 12.527 12.000 11.473 10.909
340 13.168 12.564 12.000 11.436 10.832
350 13.207 12.583 12.000 11.417 10.793
360 13.206 12.582 12.000 11.418 10.794
0 Comments
Accepted Answer
Aurelien Queffurust
on 2 Jan 2012
Let's call your example toto.txt:
You can write :
fid = fopen('toto.txt','r')
A = textscan(fid,'%f %f %f %f %f %f','headerlines',2)
fclose(fid);
max_value_per_column = max(cell2mat(A(:,2:end))); % I skip the first column
min_value_per_columnmin(cell2mat(A(:,2:end))); % I skip the first column
0 Comments
More Answers (3)
Jose Jeremias Caballero
on 2 Jan 2012
%Hi.
clear all
dn = 1:365;
sd = 23.45*sind(360*(dn+284)/365);
for L = -20:10:20
H = (180/pi)*(2/15)*(acos(-tand(L)*tand(sd)));
if L == -20
HN20 = H;
elseif L == -10
HN10 = H;
elseif L == 0
H0 = H;
elseif L == 10
HP10 = H;
elseif L == 20
HP20 = H;
end
end
fout=fopen('max_min1.txt','w');
fprintf(fout,' dn L(-20) L(-10) L(0) L(10) L(20) \n');
fprintf(fout,'-------------------------------------------\n');
for j = 10:10:365
fprintf(fout,'%3d %.3f %.3f %.3f %.3f %.3f \n',j, HN20(j),...
HN10(j), H0(j), HP10(j), HP20(j));
end
fclose(fout);
fid = fopen('max_min1.txt','r') ;
A = textscan(fid,'%f %f %f %f %f %f','headerlines',2);
fclose(fid);
A=cell2mat(A);
for i=2:size(A,2)
[minimo_value(i),pos_min(i)]=min(A(:,i));
[maximo_value(i),pos_max(i)]=max(A(:,i));
end
fprintf('dn minimun value dn maximum value\n')
for i=2:size(A,2)
fprintf('%3d\t\t%6.3f\t\t%3d\t\t%6.3f\n',10*pos_min(i),minimo_value(i),...
10*pos_max(i),maximo_value(i))
end
EXECUTION
>> textscan2
dn minimun value dn maximum value
170 10.790 350 13.207
170 11.416 350 12.583
10 12.000 10 12.000
350 11.417 170 12.584
350 10.793 170 13.210
0 Comments
Jose Jeremias Caballero
on 2 Jan 2012
%============================================
%Hi.
clear all
dn = 1:365;
sd = 23.45*sind(360*(dn+284)/365);
for L = -20:10:20
H = (180/pi)*(2/15)*(acos(-tand(L)*tand(sd)));
if L == -20
HN20 = H;
elseif L == -10
HN10 = H;
elseif L == 0
H0 = H;
elseif L == 10
HP10 = H;
elseif L == 20
HP20 = H;
end
end
fout=fopen('textscan3.txt','w');
fprintf(fout,' dn L(-20) L(-10) L(0) L(10) L(20) \n');
fprintf(fout,'-------------------------------------------\n');
for j = 1:365
fprintf(fout,'%3d %.3f %.3f %.3f %.3f %.3f \n',j, HN20(j),...
HN10(j), H0(j), HP10(j), HP20(j));
end
fclose(fout);
fid = fopen('textscan3.txt','r') ;
A = textscan(fid,'%f %f %f %f %f %f','headerlines',2);
fclose(fid);
A=cell2mat(A);
for i=2:size(A,2)
[minimo_value(i),pos_min(i)]=min(A(:,i));
[maximo_value(i),pos_max(i)]=max(A(:,i));
end
fout1=fopen('max_min_3.txt','w');
fprintf(fout1,'dn minimun value dn maximum value\n');
fprintf(fout1,'---------------------------------------\n');
for i=2:size(A,2)
fprintf(fout1,'%3d\t\t%6.3f\t\t%3d\t\t%6.3f\n',pos_min(i),minimo_value(i),...
pos_max(i),maximo_value(i));
end
fclose(fout1);
type max_min_3.txt
%======================================
>> %EXECUTION
>> textscan3
dn minimun value dn maximum value
---------------------------------------
171 10.789 353 13.211
171 11.415 353 12.585
1 12.000 1 12.000
353 11.415 171 12.585
353 10.789 171 13.211
0 Comments
See Also
Categories
Find more on Printing and Saving in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!