Represent coordinates in Matlab vector

Asked

Viewed 940 times

0

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

Given an input n will be constructed an array of coordinates of size n. 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 n array positions.

1 answer

0


A single vector for the x and y positions, I think the right one would be a matrix with the positions, but in any case to simplify already thought of using something similar to 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 just edited, you can allocate the size of each vector in the memory Matlab 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 coordinated instance to your other class.

  • It’s not feasible to do this in my case. I got a solution using the java.awt.Point class of java in a java array in Matlab, but I would still like to build the coordinate vector in Matlab itself without relying on Java.

  • is that it was not very clear your problem, a coordinate vector of space for many types of implementations,s I advise you to demonstrate an example of how you expect this vector to be ...

  • All right I’ll edit.

  • give a look if this is more or less what you need ..

  • I had to use a builder for each class, and if the other had not built anything. However for the effort I will score you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.