45 views (last 30 days)
Show older comments
Amron Rosyada about 21 hours ago
-
-
Link
Direct link to this question
https://www.mathworks.com/matlabcentral/answers/2123561-how-to-get-variable-of-function-in-for-loop
Answered: Steven Lord about 11 hours ago
Open in MATLAB Online
Hello, someone please help me.
I created a polynomial function f(x,y) using two variables. I varied the values of both variables to get the maximum f(x,y) using for loop and i got it. But, i dont know how to get or display the values of the x and y that made it. can you tell me the syntax to get them? thanks!!
x = 1:20;
y = 1:13;
f = [];
for i = 1:length(x), j = 1:length(j);
f(i,j) = x(i).^2 - x(i) -2*y(j).^2 + y(j) -25;
end
fmax = max(max(f));
display(fmax)
fmax = 354
2 Comments Show NoneHide None
Show NoneHide None
VBBV about 21 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2123561-how-to-get-variable-of-function-in-for-loop#comment_3174756
Open in MATLAB Online
Do you mean display x and y values only or f values ?
x = 1:20 % delete the semi colon
x = 1x20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = 1:13 %
y = 1x13
1 2 3 4 5 6 7 8 9 10 11 12 13
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
disp(x)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
disp(y)
1 2 3 4 5 6 7 8 9 10 11 12 13
Amron Rosyada about 8 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2123561-how-to-get-variable-of-function-in-for-loop#comment_3174766
Edited: Amron Rosyada about 8 hours ago
you didnt get my question. i got the fmax. but at which variable of x and y that excactly subtituted in that f function. i mean, when (the value of x and y subtituted) fmax happens?
Sign in to comment.
Sign in to answer this question.
Answers (2)
Steven Lord about 2 hours ago
Open in MATLAB Online
Don't call max twice. Call max once with two output arguments and specify 'all' as the dimension over which to operate. That second output will be the linear index of the location where the maximum value returned as the first output was located. Then use ind2sub to get the row and column indices.
x = 1:20;
y = 1:13;
I've written your function as a function handle so I can call it from multiple places without retyping it.
fh = @(x, y) x.^2-x-2*y.^2+y-25;
Note that as originally written, this for loop doesn't do what you think it does. You can't have one for loop iterate over two arrays like this.
% f = [];
% for i = 1:length(x), j = 1:length(j);
% f(i,j) = fh(x(i), y(j));
% end
You need two for loops. Or you could use the vectorized approach others have suggested. But there's no need to grid the data explicitly as others have done; you can take advantage of the implicit expansion behavior of the basic operators in MATLAB like plus.
f = fh(x, y.')
f = 13x20
-26 -24 -20 -14 -6 4 16 30 46 64 84 106 130 156 184 214 246 280 316 354 -31 -29 -25 -19 -11 -1 11 25 41 59 79 101 125 151 179 209 241 275 311 349 -40 -38 -34 -28 -20 -10 2 16 32 50 70 92 116 142 170 200 232 266 302 340 -53 -51 -47 -41 -33 -23 -11 3 19 37 57 79 103 129 157 187 219 253 289 327 -70 -68 -64 -58 -50 -40 -28 -14 2 20 40 62 86 112 140 170 202 236 272 310 -91 -89 -85 -79 -71 -61 -49 -35 -19 -1 19 41 65 91 119 149 181 215 251 289 -116 -114 -110 -104 -96 -86 -74 -60 -44 -26 -6 16 40 66 94 124 156 190 226 264 -145 -143 -139 -133 -125 -115 -103 -89 -73 -55 -35 -13 11 37 65 95 127 161 197 235 -178 -176 -172 -166 -158 -148 -136 -122 -106 -88 -68 -46 -22 4 32 62 94 128 164 202 -215 -213 -209 -203 -195 -185 -173 -159 -143 -125 -105 -83 -59 -33 -5 25 57 91 127 165
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[fmaxval, fmaxloc] = max(f, [], 'all')
fmaxval = 354
fmaxloc = 248
What does that location of 248 correspond to in terms of x and y?
[row, col] = ind2sub(size(f), fmaxloc)
row = 1
col = 20
fprintf("Maximum of f is %d at x = %d, y = %d.\n", fmaxval, col, row)
Maximum of f is 354 at x = 20, y = 1.
Note that the index into x is the col index and the index into y is the row index. Let's check by evaluating the function once more at those specific values of x and y.
fh(x(col), y(row))
ans = 354
That matches the maximum value found by max (and by inspection of the displayed f array.)
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
VBBV about 9 hours ago
Open in MATLAB Online
x = 1:20;
y = 1:13;
f = [];
for i = 1:length(x),
for j = 1:length(y);
f(i,j) = x(i).^2 - x(i) -2*y(j).^2 + y(j) -25;
end
end
f
f = 20x13
-26 -31 -40 -53 -70 -91 -116 -145 -178 -215 -256 -301 -350 -24 -29 -38 -51 -68 -89 -114 -143 -176 -213 -254 -299 -348 -20 -25 -34 -47 -64 -85 -110 -139 -172 -209 -250 -295 -344 -14 -19 -28 -41 -58 -79 -104 -133 -166 -203 -244 -289 -338 -6 -11 -20 -33 -50 -71 -96 -125 -158 -195 -236 -281 -330 4 -1 -10 -23 -40 -61 -86 -115 -148 -185 -226 -271 -320 16 11 2 -11 -28 -49 -74 -103 -136 -173 -214 -259 -308 30 25 16 3 -14 -35 -60 -89 -122 -159 -200 -245 -294 46 41 32 19 2 -19 -44 -73 -106 -143 -184 -229 -278 64 59 50 37 20 -1 -26 -55 -88 -125 -166 -211 -260
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fmax = max(max(f));
display(fmax)
fmax = 354
6 Comments Show 4 older commentsHide 4 older comments
Show 4 older commentsHide 4 older comments
VBBV 1 minute ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2123561-how-to-get-variable-of-function-in-for-loop#comment_3174746
You can follow the for loop syntax given below
https://in.mathworks.com/help/matlab/ref/for.html
Amron Rosyada about 21 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2123561-how-to-get-variable-of-function-in-for-loop#comment_3174751
thanks, but my question is how to get the variables ( x and y) that build the fmax?
VBBV about 21 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2123561-how-to-get-variable-of-function-in-for-loop#comment_3174771
Edited: VBBV about 8 hours ago
Open in MATLAB Online
x = 1:20;
y = 1:13;
f = [];
for i = 1:length(x),
for j = 1:length(y);
f(i,j) = x(i).^2 - x(i) -2*y(j).^2 + y(j) -25;
end
end
disp(f)
-26 -31 -40 -53 -70 -91 -116 -145 -178 -215 -256 -301 -350 -24 -29 -38 -51 -68 -89 -114 -143 -176 -213 -254 -299 -348 -20 -25 -34 -47 -64 -85 -110 -139 -172 -209 -250 -295 -344 -14 -19 -28 -41 -58 -79 -104 -133 -166 -203 -244 -289 -338 -6 -11 -20 -33 -50 -71 -96 -125 -158 -195 -236 -281 -330 4 -1 -10 -23 -40 -61 -86 -115 -148 -185 -226 -271 -320 16 11 2 -11 -28 -49 -74 -103 -136 -173 -214 -259 -308 30 25 16 3 -14 -35 -60 -89 -122 -159 -200 -245 -294 46 41 32 19 2 -19 -44 -73 -106 -143 -184 -229 -278 64 59 50 37 20 -1 -26 -55 -88 -125 -166 -211 -260 84 79 70 57 40 19 -6 -35 -68 -105 -146 -191 -240 106 101 92 79 62 41 16 -13 -46 -83 -124 -169 -218 130 125 116 103 86 65 40 11 -22 -59 -100 -145 -194 156 151 142 129 112 91 66 37 4 -33 -74 -119 -168 184 179 170 157 140 119 94 65 32 -5 -46 -91 -140 214 209 200 187 170 149 124 95 62 25 -16 -61 -110 246 241 232 219 202 181 156 127 94 57 16 -29 -78 280 275 266 253 236 215 190 161 128 91 50 5 -44 316 311 302 289 272 251 226 197 164 127 86 41 -8 354 349 340 327 310 289 264 235 202 165 124 79 30
fmax = max(max(f))
fmax = 354
[r,c] = find(f==fmax)
r = 20
c = 1
display(x(r))
20
disp(y(c))
1
VBBV 1 minute ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2123561-how-to-get-variable-of-function-in-for-loop#comment_3174776
The x and y values are 20 and 1 that produce max value in f
Akira Agata about 7 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2123561-how-to-get-variable-of-function-in-for-loop#comment_3174791
Open in MATLAB Online
+1
By using meshgrid, you can avoid for-loop, like:
x = 1:20;
y = 1:13;
% Create x- and y-grid
[xGrid, yGrid] = meshgrid(x, y);
% Calculate f
f = xGrid.^2 - xGrid - 2*(yGrid.^2) + yGrid -25;
% Find max(f) and it's linear index
[fmax, idx] = max(f(:));
% Show the result
sprintf('max(f) = %d', fmax)
ans = 'max(f) = 354'
sprintf('(x,y) = (%d, %d)', xGrid(idx), yGrid(idx))
ans = '(x,y) = (20, 1)'
% Visualize f
figure
surf(xGrid, yGrid, f)
VBBV about 13 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2123561-how-to-get-variable-of-function-in-for-loop#comment_3175126
@akira Agata what does "+1' mean ?
Sign in to comment.
Sign in to answer this question.
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
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- 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
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office