I need to evaluate the function in each combination of (x1, x2, x3), that is:
for i=1:11
for j=1:11
for k=1:11
z(i,j,k)=feval(f,x1(i),x2(j),x3(k));
end
end
end
This evaluation gives us an NxNxN matrix (cell?). I don't know what it's called hahahaha
[sfx, ind]=sort(z(:));
we selected the t smallest function values
sfx=sfx(1:t);
And the indexes corresponding to the t smallest function values
ind=ind(1:t);
Does anyone have any idea how I can return the values of x1, x2 and x3 corresponding to these t smallest function values?

4 Kommentare

Rik
Rik am 27 Mär. 2020
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.
Everton Silva
Everton Silva am 27 Mär. 2020
Thanks for your suggest. It's my first time that I asked here, the next time I'll write in this right way. ;-)
Guillaume
Guillaume am 27 Mär. 2020
Notes:
z(i,j,k)=feval(f,x1(i),x2(j),x3(k));
can be written more simply as
z(i,j,k)=f(x1(i),x2(j),x3(k));
assuming that f is a function handle.
Also, depending on f you may not need the loops at all.
Steven Lord
Steven Lord am 27 Mär. 2020
To compute the smallest values you can use the mink function.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Rik
Rik am 27 Mär. 2020
Bearbeitet: Rik am 28 Mär. 2020

1 Stimme

The indices you get in your last operation can be converted to subscripts, which will correspond to your three inputs.
ind=ind(1:t);
[x1_ind,x2_ind,x3_ind]=ind2sub(size(z),ind);
x1=x1(x1_ind);
x2=x1(x2_ind);
x3=x1(x3_ind);

4 Kommentare

Everton Silva
Everton Silva am 27 Mär. 2020
When I write this commands it's doesn't work. Appears this message: ''Error using sub2ind
Too many output arguments.''
Walter Roberson
Walter Roberson am 27 Mär. 2020
[x1_ind,x2_ind,x3_ind]=ind2sub(size(z),ind);
Everton Silva
Everton Silva am 27 Mär. 2020
Thank you so much.
Rik
Rik am 28 Mär. 2020
@Walter thanks for catching the mistake
@Everton you're welcome

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Torsten
Torsten am 27 Mär. 2020
Bearbeitet: Torsten am 27 Mär. 2020

0 Stimmen

[X1,X2,X3]=ndgrid(x1,x2,x3);
Z=f(X1,X2,X3); %assumes f is vectorized
M=[X1,X2,X3,Z];
[~,idx]=sort(M(:,4));
sortedM=M(idx,:);
X1s=sortedM(1:s,1);
X2s=sortedM(1:s,2);
X3s=sortedM(1:s,3);

1 Kommentar

Everton Silva
Everton Silva am 27 Mär. 2020
For example, if I write in the command window
t=11;
f=@(x1,x2,x3)(x1+x2+x3);
x1=0:0.1:1;
x2=0:0.1:1;
x3=0:0.1:1;
[X1,X2,X3]=ndgrid(x1,x2,x3);
Z=f(X1,X2,X3);
M=[X1,X2,X3,Z];
[~,idx]=sort(M(:,4));
sortedM=M(idx,:);
X1s=sortedM(1:t,1);
X2s=sortedM(1:t,2);
X3s=sortedM(1:t,3);
X=[X1s,X2s,X1s]
returns
X=
0 0 0
0.1000 0.1000 0.1000
0.2000 0.2000 0.2000
0.3000 0.3000 0.3000
0.4000 0.4000 0.4000
0.5000 0.5000 0.5000
0.6000 0.6000 0.6000
0.7000 0.7000 0.7000
0.8000 0.8000 0.8000
0.9000 0.9000 0.9000
1.0000 1.0000 1.0000

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 27 Mär. 2020

Kommentiert:

Rik
am 28 Mär. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by