Assigning the Values to 2-D Matrix which has 2 Elements in Each Point
Show older comments
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
tinkyminky93
on 29 Mar 2022
X = zeros(3);
Y = zeros(3);
xvec = [0,100,200];
yvec = [500,250,0];
X = repmat(xvec,3,1)
Y = repmat(yvec,3,1).'
tinkyminky93
on 29 Mar 2022
Torsten
on 29 Mar 2022
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.
tinkyminky93
on 29 Mar 2022
Edited: tinkyminky93
on 29 Mar 2022
Torsten
on 29 Mar 2022
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)]
tinkyminky93
on 29 Mar 2022
tinkyminky93
on 29 Mar 2022
Torsten
on 29 Mar 2022
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.
tinkyminky93
on 29 Mar 2022
Torsten
on 29 Mar 2022
That's where the problems start and why I try to avoid using cells.
Try
A{i,j} = [xvec(j),yvec(i)];
Torsten
on 29 Mar 2022
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
tinkyminky93
on 29 Mar 2022
Torsten
on 29 Mar 2022
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.
Answers (1)
David Hill
on 29 Mar 2022
Sounds like you need two matrices. A x matrix and a y matrix.
[x,y]=meshgrid(0:100:200,0:250:500);
4 Comments
tinkyminky93
on 29 Mar 2022
Edited: tinkyminky93
on 29 Mar 2022
tinkyminky93
on 29 Mar 2022
tinkyminky93
on 29 Mar 2022
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!