How to make matrix calculations in rows?
2 views (last 30 days)
Show older comments
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 Comment
Dyuman Joshi
on 28 Nov 2023
Depending upon the transformation, you could vectorize your code as well.
See - Vectorization
Answers (1)
Rohit Kulkarni
on 28 Nov 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!
See Also
Categories
Find more on Creating and Concatenating Matrices 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!