Represent vector coordinates of Matlab

0

I'm having problems creating a vector of objects in Matlab, the idea being that it is a vector of coordinates, i.e. the positions x and y. The goal is to have a vector to access the coordinates in the loops easily, and have the atomicity of the data to have consistency of which position is x or y. I think you have to use constructor.

Given an input n, an array of coordinates of size n will be constructed. In which each element will have its own x and y. Ex:

n=2;
vetor(n)=coord(0,0);
vetor(1).x=5;vetor(1).y=3;
vetor(2).x=2;vetor(2).y=4;

The main issue is that I want to leave the vector instance in another class, which will have to have in the constructor the allocation of this array of n positions.

    
asked by anonymous 18.05.2014 / 02:49

1 answer

0

A single vector for the x and y positions, I think the right would be an array with the positions, but in any case for simplicity already thought of using something like hash or dictionaries (scalar structure), you can create a dictionary called coordinate , within it create the corresponding X and Y coordinates, you can use something like this:

%coloque os valores correspondentes a suas coordenadas, veja um exemplo
coordenada.X=[1 2 3 4 5]
coordenada.Y=[1 2 3 4 5]
%agora faça um loop para acessar suas coordenadas assim:
for i=1:length(coordenada.X)
     X=coordenada.X(i)
     Y=coordenada.Y(i)
 end

Following the example you showed and you just edited, you can allocate the size of each vector in the matlab in memory using zeros, see:

n=2;

%alocando o espaço necessário
coordenada.X=zeros(n,1);
coordenada.Y=zeros(n,1);

%agora popule suas coordenadas
coordenada.X(1)=5; coordenada.Y(1)=3
.
.
.

In this case, just pass the coordinate instance to your other class.

    
18.05.2014 / 03:39