You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Finding minimum value by using min()
2 views (last 30 days)
Show older comments
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
Image Analyst
on 7 Dec 2018
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
Sushree Patra
on 7 Dec 2018
Thank you very much for the solution. It helps me to find the result.
Steven Lord
on 7 Dec 2018
Alternately you could use the mink function.
Sushree Patra
on 7 Dec 2018
Thank you sir Steven,
I need a source code for Free search krill herd algorithm for global optimization.
Can you help me sir?
Image Analyst
on 7 Dec 2018
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.
Sushree Patra
on 7 Dec 2018
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.
Image Analyst
on 7 Dec 2018
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).
More Answers (1)
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
Sushree Patra
on 7 Dec 2018
showing error
Subscripted assignment dimensions mismatch
Sushree Patra
on 7 Dec 2018
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
madhan ravi
on 7 Dec 2018
attach fitness as .mat file
Sushree Patra
on 7 Dec 2018
attaching the code named as rly_sen.m
Jan
on 7 Dec 2018
@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.
madhan ravi
on 7 Dec 2018
I just ran your code but I didn't get any errors?
Sushree Patra
on 7 Dec 2018
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)
madhan ravi
on 7 Dec 2018
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
Sushree Patra
on 7 Dec 2018
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]);
madhan ravi
on 7 Dec 2018
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
>>
Sushree Patra
on 7 Dec 2018
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
Sushree Patra
on 7 Dec 2018
is it calculated correctly the minimum fitness value
madhan ravi
on 7 Dec 2018
Ofitness is a 3D matrix , which minimum do you want to return? minimum of each row/column or the minimum of each page?
Sushree Patra
on 7 Dec 2018
minimum 10 fitness value of each column of each page
Sushree Patra
on 7 Dec 2018
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
madhan ravi
on 7 Dec 2018
min(Ofitness) % that's it outside loop
Sushree Patra
on 7 Dec 2018
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
Sushree Patra
on 7 Dec 2018
Among all best 10 minimum might be calculate by sort them and choose first 10
Sushree Patra
on 7 Dec 2018
now minFit = 1*10*13double
my objective =1*10double
madhan ravi
on 7 Dec 2018
Edited: madhan ravi
on 7 Dec 2018
minFit=squeeze(min(Ofitness,[],[1 2])).' % after this I don't have any clue what you want
Walter Roberson
on 7 Dec 2018
madhan, the syntax min(Array,[],[vector]) is new as of r2018b .
madhan ravi
on 7 Dec 2018
Thank you so much sir Walter , is there any alternatives for it in older versions?
Walter Roberson
on 7 Dec 2018
multiple min calls or reshape followed by min
madhan ravi
on 7 Dec 2018
Thank you sir Walter ?
Sushree Patra
on 7 Dec 2018
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
Sushree Patra
on 7 Dec 2018
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
madhan ravi
on 7 Dec 2018
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
Sushree Patra
on 7 Dec 2018
Edited: Sushree Patra
on 7 Dec 2018
(,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]
madhan ravi
on 7 Dec 2018
what does A' and B' represent? the pages?
Sushree Patra
on 7 Dec 2018
yes that are different pages
madhan ravi
on 7 Dec 2018
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(:))]
Sushree Patra
on 7 Dec 2018
Thank you sir but in my code how it will fix
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)