How to make matrix calculations in rows?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Dyuman Joshi
2023-11-28
Depending upon the transformation, you could vectorize your code as well.
See - Vectorization
回答(1 个)
Rohit Kulkarni
2023-11-28
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!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!