Assigning the Values to 2-D Matrix which has 2 Elements in Each Point

Hello, I am trying to assign the values to a matrix. This matrix is a 3x3 matrix but every point has 2 elements, which are the coordinates of a point, in other words x and y values.
For example I want this matrix to be in the form of:
[0 500] [100 500] [200 500]
[0 250] [100 250] [200 250]
[0 0] [100 0] [200 0]
The Y values are 0, 250, 500 and the X values are 0, 100, 200 respectively. I tried to assign values by using for loop but I could not achieve the assignment of 2nd element. Can you help me? Thank you.

16 Comments

Yes, form two matrices named X and Y, one with the X values and the other with the Y values.
If you do it the way you plan, you will have to define the matrix as a cell array, and you shouldn't make things artificially complicated.
I already did that but I am confused about the indexing. For example I want to reach the position of [100 250]. What should I write? If I write like A = [x y], when I write A(2,2) it will give me 1 element but I need 2 elements which are 100 and 250. :)
X = zeros(3);
Y = zeros(3);
xvec = [0,100,200];
yvec = [500,250,0];
X = repmat(xvec,3,1)
Y = repmat(yvec,3,1).'
There isn't any problem about creating a matrix. The problem is, I want both X and Y values of an element. The thing that I want to see is A(3,1) = (0, 200) or A(4,1) = (0, 300) while X = 0 and Y = 200, 300
You can't. A(3,1) is always a scalar, i.e. one single value.
But usually
element = [X(i,j),Y(i,j)]
is sufficient to work with.
Yes that is the point sir, thanks. From your answer, how can I assign x and y values by for loop?
for i = 1:1:3
for j = 1:1:3
element(i,:) = element(i,:) + 100
element(:,j) = element(:,j) + 250
end
end
something like that sir?
Say you could generate a matrix like
A = [[0 500] [100 500] [200 500]
[0 250] [100 250] [200 250]
[0 0] [100 0] [200 0]];
Now everywhere where you want to access A(i,j), just write [x(i,j),y(i,j)] where the two matrices x and y are obtained by
[x,y]=meshgrid(0:100:200,0:250:500);
E.g. if you want to add A(1,1) to A(2,3), you write
[x(1,1),y(1,1)] + [x(2,3),y(2,3)]
I understood sir but this is not the answer of my last question about for loop. Can you answer it?
You want to know how to use a loop to create the matrices X and Y from the vectors
xvec = [0 100 200] and
yvec = [500 250 0] ?
Nobody uses a loop to do that - everybody uses meshgrid or the assignment
X = repmat(xvec,3,1)
Y = repmat(yvec,3,1).'
Okey so much thanks sir I got it, but shouldn't be a method to create a matrix with for loop? Is it not possible or is it unnecessary? :)
A = cell(3,3);
xvec = [0,100,200];
yvec = [500,250,0];
for i = 1:3
for j = 1:3
A(i,j) = [xvec(j),yvec(i)];
end
end
Here you have a matrix A where each entry is made up of a tuple, like you wanted.
This matrix is of type cell.
If you want to work with it, try.
But don't complain if you run into problems.
So operations like adding, subtracting, multiplying etc. have to be defined by yourself.
I have an error:
"Conversion to cell from double is not possible"
That's where the problems start and why I try to avoid using cells.
Try
A{i,j} = [xvec(j),yvec(i)];
One last thing:
If the matrix entries are complex numbers, you can of course work with tuples as matrix elements:
A = zeros(3,3);
xvec = [0,100,200];
yvec = [500,250,0];
for i = 1:3
for j = 1:3
A(i,j) = complex(xvec(j),yvec(i));
end
end
I think this is not the correct solution, complex number representation should not be used as coordinates sir. Thanks for your effort. At least I understand some concepts.
I just wanted to say that - although complex numbers are in principle tuples of real numbers - they can be assigned to matrix elements. Also, rules how to add, subtract, multiply and divide such matrices are already implemented.
For your case, when you work with cell arrays - these rules are not yet available and you had to define and implement them on your own.

Sign in to comment.

Answers (1)

Sounds like you need two matrices. A x matrix and a y matrix.
[x,y]=meshgrid(0:100:200,0:250:500);

4 Comments

Yes it is true but for example I will assign an amplitude to an element. How can I reach the elements of this array? I want to create this matrix with for loop not meshgrid, is there any other method?
I tried this code and for example I want the point (100,250). What should I write? I mean like A(2,2)
I already gave you the answer above.
Everywhere where you would use A(i,j), you write [x(i,j),y(i,j)].
I see sir, I understood. I asked one more and the last question above.

Sign in to comment.

Products

Release

R2021b

Asked:

on 29 Mar 2022

Commented:

on 29 Mar 2022

Community Treasure Hunt

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

Start Hunting!