MATLAB - Using for loops find all the combinations of x^2 + y -


i have 2 loops this:

for x = 1:1:15     y = 1:1:15         values(x,y) = x^2 + y     end end 

this allows me calculate x^2 + y every combination of x , y if integers.

however, if want calculate x^2 + y decimals well?

so this:

for x = 0:0.1:15     y = 0:0.1:15         ????? = x^2 + y     end end 

could me find method can calculate possibilities of x^2 + y if x , y decimals cannot used index references anymore?

use:

[x y] = ndgrid(0:0.1:15); values = x.^2 + y; 

issues other answers:

  1. @inigo's answer change order of x , y compared initial example (by using meshgrid rather ndgrid.
  2. @nominsim's answer has go effort d_x x
  3. @mecid's answer has count columns , rows separately (also there no ++ operator in matlab). if go down @mecid's route use following.
x = 0:.1:15; y = 0:.1:15; values = zeros(numel(x),numel(y)); xnum = 1:numel(x)     ynum = 1:numel(y)         values(xnum,ynum) = x(xnum)^2 + y(ynum);     end end 

since generated discussion, documentation (within matlab, not in online documentation) on difference between meshgrid , ndgrid:

meshgrid ndgrid except order of first 2 input , output arguments switched (i.e., [x,y,z] = meshgrid(x,y,z) produces same result [y,x,z] = ndgrid(y,x,z)) ... meshgrid limited 2d or 3d.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -