Finding minimum value by using min()

I want to find minimum 10 value
i wrote
for j= 1:10
for i= 1:10
for k = 1:13
fitness (j,i,k)=(........);%10*10*13 double
end
end
minfit(j) = min(fitness(j,:));
end
disp (minfit);
in min function it is unable to find
minimum value of .
please helpme i am not able to solfitnesse

 Accepted Answer

To find the 10 minimum values of any array, just sort:
sortedValues = sort(fitness(:), 'descend');
tenMinValues = sortedValues(1:10);
If you don't want duplicated values, use unique
sortedValues = unique(fitness(:));
tenMinValues = sortedValues(1:10);

6 Comments

Thank you very much for the solution. It helps me to find the result.
Alternately you could use the mink function.
Thank you sir Steven,
I need a source code for Free search krill herd algorithm for global optimization.
Can you help me sir?
How is that different than the numerous other animal herd/swarm/flock/etc. algorithms?
There is no Krill Toolbox so if you can't find it in the File Exchange, and can't use one of the other PSO (Particle Swarm Optimization) algorithms, then you could ask the author (whoever invented it) for code, or write it yourself from lower level functions.
Thank you sir for the response.
Well, it is different in case of movement of krill and finding the next generation they specifically use free search operator and opposition based method for population intialization.
And I asked to the author but they don't reply.
So I am trying by myself.
Please help me if I am unable to solve.
I've done all the help I can do. See the FAQ
I'm not an expert in PSO-type algorithms, and haven't used them (not needed to so far).

Sign in to comment.

More Answers (1)

madhan ravi
madhan ravi on 7 Dec 2018
Edited: madhan ravi on 7 Dec 2018
minFit=squeeze(min(Ofitness,[],[1 2])).'
minFit=squeeze(min(min(Ofitness))).' % in older versions of matlab thank you sir Walter

32 Comments

showing error
Subscripted assignment dimensions mismatch
showing error
error using min
Dimension argument must be a positive integer scalar within index range.
i placed min function outside of all theloop 3 for
attach fitness as .mat file
attaching the code named as rly_sen.m
@Sushree Patra: Please post the code. It is strange, that you get two different errors. Without seeing the code, it is impossible to fix it.
I just ran your code but I didn't get any errors?
ok i post it
clc;
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
figure;
imshow('pc3.png');
hold on
hold on
%legend('Relay', 'Sink', 'Sensor');
%
x1 = [105,107,58,58,75,146,118,18,48,55,108,80,80];
y1 = [190,280,190,280,145,153,103,153,103,65,65,50,10];
hold on
A=100;
B=140;
plot(A,B, 'mo-', 'MarkerSize', 10,'LineWidth', 2);
hold on
hold on
% Make 75 points in set #2
Xmin = 60;
Xmax = 99;
x2 = Xmin+rand(1,50)*(Xmax-Xmin);
Ymin = 10;
Ymax= 260;
y2 = Ymin+rand(1,50)*(Ymax-Ymin);
plot(x2, y2, 'b*');
%numPoints2 = 50;
%x2 = 167*rand(numPoints2, 1);
%y2 = 302*rand(numPoints2, 1);
% Plot set 1 in red
plot(x1, y1, 'r.', 'MarkerSize', 13);
% Plot set 2 in blue
hold on;
plot(x2, y2, 'b*', 'MarkerSize', 5);
grid on;
for i=1:13
for j=1:50
%Find distances between every point in set 1 to every point in set #2.
distances(i,j) = pdist2([x1(i),y1(i)], [x2(j), y2(j)], 'euclidean');
end
minDistance(i) = min(distances(i,:));
end
% Find min distance
disp (distances);
dist1 = sqrt((x2-A).^2+(y2-B).^2)
%disp (dist1);
dist2 =sqrt((x1-A).^2+(y1-B).^2)
disp (minDistance);
a=1:40;
for i = 1:10
pop(i,:) = a(randperm(numel(a),10))
end
for i = 1:10
opo_pop(i,:) = 41-pop(i,:)
end
%Est(13,5)=zeros();
%for i=1:13
% b=1;
%for k=1:50
%for j=10*k-9:10*k
%if mod(k,10)==0
%b=b+1;
%end
s=250;
Et=16.7;
Eamp=1.97;
N=10;
data_agg=1;
Er = 36.1;
d0=30;
c=10;
for k = 1:13
for j = 1:10
for i = 1:10
Ets(k,j,i)=s*(Et+Eamp*(distances(k,pop(j,i)).^2));
%Es(i,b)=Est(i,b)+Et(i,k);
end
end
end
disp (Ets);
for k = 1:13
for j = 1:10
for i = 1:10
OEts(k,j,i)=s*(Et+Eamp*(distances(k,opo_pop(j,i)).^2));
%Es(i,b)=Est(i,b)+Et(i,k);
end
end
end
disp (OEts);
%disp(Es);
for j=1:10
for i=1:10
for k= 1:1
Etf(j,i,k)= N*data_agg*Et*(s*dist1(k,pop(j,i)))+N*s*data_agg*Et;
Erf = (N-1)*Er*s;
%Ef_total(i,k)=Etf(i,k)+Erf
end
end
end
Ef_total =Etf + Erf
for j=1:10
for i=1:10
for k= 1:1
OEtf(j,i,k)= N*data_agg*Et*(s*dist1(k,opo_pop(j,i)))+N*s*data_agg*Et;
OErf = (N-1)*Er*s;
%Ef_total(i,k)=Etf(i,k)+Erf
end
end
end
OEf_total =OEtf + OErf
for j=1:10
for i =1:10
for k = 1:13
fitness(j,i,k)= (c*pop(j,i)*1)+(c*(Ets(k,j,i)*1+Ef_total(j,i,1)*1))+(c*sqrt((distances(k,pop(j,i))-d0).^2))+(c*1);
end
end
%minFit(j) = min(fitness(j,:,:));
end
minFit = min(fitness,[],[1,2]);
disp(fitness);
for j=1:10
for i =1:10
for k = 1:13
Ofitness(j,i,k)= (c*opo_pop(j,i)*1)+(c*(OEts(k,j,i)*1+OEf_total(j,i,1)*1))+(c*sqrt((distances(k,opo_pop(j,i))-d0).^2))+(c*1);
end
end
OminFit(j) = min(Ofitness(j,:));
end
disp(Ofitness);
disp(minFit);
minfit=minFit(:);
disp(OminFit);
ominfit=OminFit(:);
Int_Fit = [minfit;ominfit]
intfitness = sort(Int_Fit)
pc3.png
REmove this section of the code!:
for j=1:10
for i =1:10
for k = 1:13
Ofitness(j,i,k)= (c*opo_pop(j,i)*1)+(c*(OEts(k,j,i)*1+OEf_total(j,i,1)*1))+(c*sqrt((distances(k,opo_pop(j,i))-d0).^2))+(c*1);
end
end
OminFit(j) = min(Ofitness(j,:));
end
Error using min
Dimension argument must be a positive integer scalar within indexing range.
Error in rly_sen (line 128)
minFit = min(fitness,[],[1,2]);
wierd this is what i got:
>> minFit
minFit(:,:,1) =
14831476.9829354
minFit(:,:,2) =
52886274.7233204
minFit(:,:,3) =
20688650.8562262
minFit(:,:,4) =
53651138.6228232
minFit(:,:,5) =
7829219.62925941
minFit(:,:,6) =
18287522.4496381
minFit(:,:,7) =
10960447.7077643
minFit(:,:,8) =
27724536.6404127
minFit(:,:,9) =
19579242.1922717
minFit(:,:,10) =
29888312.5238552
minFit(:,:,11) =
24860479.583199
minFit(:,:,12) =
31616660.2405204
minFit(:,:,13) =
47582942.7897235
>>
if i am usin min function like this
minFit(j) = min(Ofitness(j,:));
then no error but it is not calculate the minimum value correctly
is it calculated correctly the minimum fitness value
Ofitness is a 3D matrix , which minimum do you want to return? minimum of each row/column or the minimum of each page?
minimum 10 fitness value of each column of each page
first loop i.e j is for 10 set
second loop is for with in each set 10 value
thired loop is for 13 fixed node
i calculated the distance of each value with 13 fixed node of 10 set that is 10*10*13
now i have the find 10 minimum of all of the values and stored in minfit
min(Ofitness) % that's it outside loop
but it shows minimum value with respect to all the 13 node
is it possible to find only 10 minimum fitness value whhich is stortest among all
Among all best 10 minimum might be calculate by sort them and choose first 10
now minFit = 1*10*13double
my objective =1*10double
minFit=squeeze(min(Ofitness,[],[1 2])).' % after this I don't have any clue what you want
madhan, the syntax min(Array,[],[vector]) is new as of r2018b .
Thank you so much sir Walter , is there any alternatives for it in older versions?
multiple min calls or reshape followed by min
Thank you sir Walter ?
Thank you sir madhan, I apply the solution you gave but now minFit value equal to the fitness value
not find minimum of 10 set.
Let me clarify the objective
10 set
each set have 10 number
total number = 100
then that 100 connect to 13 then we find fitness and Ofitness
fitness & Ofitness = 10*10*13
now calculate only 10 minimum values of that 10 set means set 1 having 10*13 fitness we have to find the minimum among all. like wise for 10 set i.e. my 10 minimum
Please help me sir, I am also trying but recently i started matlab so kindly help me to solve the problem.
Thank you very much for your solutions and time
forget about everything demonstrate a neat and clean example with 2 by 2 matrix with 2 pages with your desired result it'll save time and avoid confusions
(,1)=
A=[1,5;6,2]
A'=[9,5;3,6]
(,2)=
B=[9,2;6,3]
B'=[7,3;8,4]
Now i have to find out 2 minimum value one from (,1)another from (,2)
min=[1,2]
what does A' and B' represent? the pages?
yes that are different pages
A(:,:,1)=[1,5;6,2];
A(:,:,2)=[9,5;3,6];
B(:,:,1)=[9,2;6,3];
B(:,:,2)=[7,3;8,4];
result=[min(A(:)) min(B(:))]
Thank you sir but in my code how it will fix

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!