- Decide if you want to analyze rows or columns.
- For each row (or column), extract the real and imaginary parts as separate vectors.
- Use the index of each row (or column) as the x-values.
- Perform linear fitting with polyfit using the x-values and the real or imaginary part as y-values.
- The slope will be the first coefficient returned by polyfit.
How to use polyfit on a matrix
88 views (last 30 days)
Show older comments
Hi,
I have a matrix of m x n complex numbers. The mesh plot looks something like this.
Can I use polyfit to find slope of these lines?
I am trying to understand "simple linear regression" example but cant wrap my head around it.
Any help is appreciated.
Thanks
0 Comments
Answers (1)
Shubham
on 1 Dec 2023
Hi Raghav,
In MATLAB, polyfit is typically used to perform polynomial fitting, which includes linear fitting (a special case where the polynomial degree is 1). However, polyfit is designed to work with vectors of x and y data points, representing the independent and dependent variables, respectively. Here is the documentation link: https://in.mathworks.com/help/matlab/ref/polyfit.html
If you have a mesh plot generated from an m x n matrix of complex numbers, and you want to find the slope of lines that could represent trends in that data, you'll first need to define what those lines represent. This is because a mesh plot of complex numbers doesn't inherently have a "slope" in the way a two-dimensional plot of y vs. x does.
Here's a general approach to finding a linear trend in a two-dimensional matrix, assuming you want to find the trend in one dimension (e.g., row-wise or column-wise):
Here's an example of how you might do this for each row:
% Assume A is your m x n matrix of complex numbers
A = rand(5, 5) + 1i * rand(5, 5); % Example matrix
% Preallocate array for slopes
slopes_real = zeros(size(A, 1), 1);
slopes_imag = zeros(size(A, 1), 1);
% Iterate over each row
for k = 1:size(A, 1)
% Use the column indices as the x-values
x = 1:size(A, 2);
% Extract the real and imaginary parts of the k-th row
y_real = real(A(k, :));
y_imag = imag(A(k, :));
% Fit a linear polynomial (slope and intercept) to the real part
p_real = polyfit(x, y_real, 1);
slopes_real(k) = p_real(1); % The slope for the real part
% Fit a linear polynomial (slope and intercept) to the imaginary part
p_imag = polyfit(x, y_imag, 1);
slopes_imag(k) = p_imag(1); % The slope for the imaginary part
end
% Now slopes_real and slopes_imag contain the slopes of the linear trend for
% the real and imaginary parts of each row, respectively.
This example assumes that the trend you're interested in is along the rows. If you're interested in the columns, you would iterate over columns instead and use the row indices as the x-values.
2 Comments
Shubham
on 4 Dec 2023
Hi Raghav,
The reason behind using real and imaginary parts separately in the previous example was based on the assumption that you might want to analyze trends in the real and imaginary components independently. However, if you're interested in the magnitude of the complex numbers, you can indeed use the absolute values (abs) instead.
If you're trying to find the slope of diagonal lines in a mesh plot of complex numbers, and those lines are a visual artifact due to the rotation of the mesh, then you are dealing with a more geometric problem rather than a simple linear regression one.
To find the slope of these diagonal lines, you'll need to:
- Determine which diagonal lines you're interested in (e.g., main diagonal, off-diagonal, etc.).
- Extract the points that lie on these diagonals.
- Calculate the slope using these points.
Here's an example of how you might calculate the slope of the main diagonal of the matrix:
% Assume A is your m x n matrix of complex numbers
A = rand(5, 5) + 1i * rand(5, 5); % Example matrix
% Extract the main diagonal
main_diag = diag(A);
% Calculate the absolute values if you're interested in the magnitude
abs_main_diag = abs(main_diag);
% Use the diagonal indices as the x-values
x = 1:length(main_diag);
% Fit a linear polynomial (slope and intercept) to the diagonal's absolute values
p = polyfit(x, abs_main_diag, 1);
% The slope of the main diagonal
slope_main_diag = p(1);
For off-diagonals, the process is similar, but you need to define which off-diagonal you're interested in and extract those points accordingly.
If you want to find arbitrary diagonal lines that are not necessarily the main or off-diagonals of the matrix, you would need to implement an algorithm to detect these lines. This could involve edge detection, Hough transform, or other image processing techniques if you're working with a visual representation of your data.
See Also
Categories
Find more on Polynomials 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!