hi everybody , i have a question please , if i have X=[1:10] and Y=[-5:5] and i want to have all the point of the plane (x,y) , what can i do in matlab to extract this point to use it

X=[1:10];
Y=[-5:5];
for i=1:10
for j=1:10

3 commentaires

Please be more clear, what do you want exactly?
Jan
Jan le 25 Fév 2019
Modifié(e) : Jan le 25 Fév 2019
I do not understand also, what "have point of the plane" means. Extract them from what?
BY the way: -5:5 is a vector already and you do not need the square brackets. And it has 11 elements, not 10.
first i need to thank you for responding on my question ,
i try now to be more clear
1) what i need is a column vector 2x1 where the first row is x and the second row is y
2) i need to repeat it to get all the point of the plane (x,y)
for example if X=[ 1 2 3 4 5 ]
Y=[ -5 -4 -3 -2 -1 0 1 2 3 4 5]
so (1,-5) (2,-5) (3,-5)(4,-5)(5,-5)
(1,-4) (2,-4) (3,-4)(4,-4)(5,-4)
(1,-3) (2,-3) (3,-3)(4,-3)(5,-3)
and so on till ..........................................(1,5) (2,5) (3,5)(4,5)(5,5)
so i need in a cartesian coordinate all the pointi (x,y) to use it in a certain iteration
so every cycle i need to extract a point (x,y) that is a column vector 2x1 where the first row is the point x and the second row is the point y to use it in a certain iteration
thank u very much

Connectez-vous pour commenter.

 Réponse acceptée

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.

6 commentaires

thank u very much , u r the best
please can u tell me how can i save the result in a matrix because he save only the last resutl (5,5)
x=[1 2 3 4 5 ];
y=-5:5;
tgt=zeros(55,2);
for i=1:length(x)
for j=1:length(y)
tgt=[x(i),y(j)]
end
end
Save what in a matrix? Just the points? E.g.,
tgt = zeros(numel(x)*numel(y),2);
k = 0;
:
% then inside the loop
k = k + 1;
tgt(k,:) = [x(i),y(j)];
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
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);
@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)];

Connectez-vous pour commenter.

Plus de réponses (1)

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

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Produits

Version

R2018b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by