How to use polyfit on a matrix

56 visualizzazioni (ultimi 30 giorni)
Raghav Rathi
Raghav Rathi il 30 Nov 2023
Commentato: Shubham il 4 Dic 2023
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

Risposte (1)

Shubham
Shubham il 1 Dic 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):
  1. Decide if you want to analyze rows or columns.
  2. For each row (or column), extract the real and imaginary parts as separate vectors.
  3. Use the index of each row (or column) as the x-values.
  4. Perform linear fitting with polyfit using the x-values and the real or imaginary part as y-values.
  5. The slope will be the first coefficient returned by polyfit.
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 Commenti
Raghav Rathi
Raghav Rathi il 1 Dic 2023
Thanks for the response @Shubham.
Like you said, mesh plot of complex numbers doesn't inherently have a "slope" and the figure showing slope is due to the rotation of the mesh.
My first aim is to find the diagonal lines first and calculate the slope. So far, I have been using mesh function and then taking 2 points on the lines to find the slope.
I tried to use your code, but it gives the slope of the entire matrix.
I guess I need to write a code that can first find the lines and then use the points on the line to calculate the slope. Instead of using polyfit on the entire dataset.
BTW what was the reason behind using real and imag parts separately? Why not abs values?
Thanks
Shubham
Shubham il 4 Dic 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:
  1. Determine which diagonal lines you're interested in (e.g., main diagonal, off-diagonal, etc.).
  2. Extract the points that lie on these diagonals.
  3. 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.

Accedi per commentare.

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by