Asked by mina massoud
on 25 Feb 2019

X=[1:10];

Y=[-5:5];

for i=1:10

for j=1:10

Answer by James Tursa
on 25 Feb 2019

Edited by James Tursa
on 25 Feb 2019

Accepted Answer

Does this do what you want?

[x,y] = meshgrid(X,Y);

result = [x(:),y(:)];

Then iterate over the rows of result.

Or you can just use your double for loop, and have

for i=1:numel(X)

for j=1:numel(Y)

point = [X(i),Y(j)];

etc.

mina massoud
on 26 Feb 2019

i tried this code but it didnt work

clear all

clc

intx=10;

inty=10;

D_x=3000;

D_y=1000;

x=[0:intx:D_x];

y=[-D_y/2:inty:D_y/2];

k=0

TgT=zeros(length(x),length(y))

for i=1:length(x)

for j=1:length(y)

k=k+1

TgT(k,:)=[x(i),y(j)]

end

end

James Tursa
on 27 Feb 2019

You didn't pre-allocate TgT to the dimensions I specified. You have this:

TgT=zeros(length(x),length(y))

But I have this:

tgt = zeros(numel(x)*numel(y),2);

Jan
on 27 Feb 2019

@mina: "it didnt work" is not useful to explain a problem. Do you get an error message (than post a complete copy) or do the result differ from your expectation (then explain both).

clear all; clc is called "cargo cult programming": It does not help to solve any problem. All it does is to waste processing time here.

A simpler method instead of the loops:

x = 0:intx:D_x; % No need for brackets

y = -D_y/2:inty:D_y/2; % x and y are vectors already

nx = numel(x);

ny = numel(y);

TgT = [replem(x.', ny, 1), repmat(y.', nx, 1)];

Sign in to comment.

Answer by madhan ravi
on 25 Feb 2019

I am not sure I fully understand but see if it does what you want

[x,y]=ndgrid(X,Y);

[x(:) y(:)] % after this it’s just matrix manipulation

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Alex Mcaulley (view profile)

Direct link to this comment:https://fr.mathworks.com/matlabcentral/answers/446906-hi-everybody-i-have-a-question-please-if-i-have-x-1-10-and-y-5-5-and-i-want-to-have-all-t#comment_674969

## Jan (view profile)

Direct link to this comment:https://fr.mathworks.com/matlabcentral/answers/446906-hi-everybody-i-have-a-question-please-if-i-have-x-1-10-and-y-5-5-and-i-want-to-have-all-t#comment_674971

## mina massoud (view profile)

Direct link to this comment:https://fr.mathworks.com/matlabcentral/answers/446906-hi-everybody-i-have-a-question-please-if-i-have-x-1-10-and-y-5-5-and-i-want-to-have-all-t#comment_674979

Sign in to comment.