Borrar filtros
Borrar filtros

How to make matrix calculations in rows?

1 visualización (últimos 30 días)
Jakub
Jakub el 28 de Nov. de 2023
Comentada: Dyuman Joshi el 28 de Nov. de 2023
I have a matrix, that consists from various columns representing coordinates and velocities. (fe column A is longitude, B latitude etc.). So one row represents complete information about one point. I want to transform these point and for the transformation I need to use matrix relations. The result is vector (dimension 3,1) for every point. How to create such a cycle, to do matrix calculation for each and every row in the previous matrix?
  1 comentario
Dyuman Joshi
Dyuman Joshi el 28 de Nov. de 2023
Depending upon the transformation, you could vectorize your code as well.

Iniciar sesión para comentar.

Respuestas (1)

Rohit Kulkarni
Rohit Kulkarni el 28 de Nov. de 2023
Hi Jacub,
As per my understanding you want to perform matrix calculations for each and every row.
In MATLAB, you can use a loop to iterate over the rows of your matrix and apply the transformation. Let's assume you have a matrix M where each row represents the different parameters of a point (e.g., longitude, latitude, velocity, etc.), and you have a transformation matrix T that is a 3xN matrix (where N is the number of columns in M). Here's how you could set up the loop in MATLAB:
% Assuming M is your matrix with points as rows
% Each row of M might look like [longitude, latitude, velocity_x, velocity_y, ...]
% Define your transformation matrix T, which should be 3xN where N is the number of columns in M
T = [...]; % Replace with your actual transformation matrix
% Initialize an empty matrix to store the results
result = zeros(size(M, 1), 3);
% Loop over each row of the matrix M
for i = 1:size(M, 1)
% Extract the point (row) from the matrix
point = M(i, :);
% Convert the point to a column vector if necessary
point_column = point(:); % Ensures the point is a column vector
% Apply the transformation matrix T to the point
transformed_point = T * point_column;
% Store the result in the corresponding row of the result matrix
result(i, :) = transformed_point';
end
% Now result is a matrix where each row is the 3x1 vector result of the transformation
In this code snippet, replace T and M with your actual transformation matrix and input matrix, respectively. The result will be a new matrix where each row contains the transformed 3x1 vector for each point in your original matrix.
Hope this helps!
  1 comentario
Jakub
Jakub el 28 de Nov. de 2023
Thank you, it helped me a lot :)
Best regards

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by