Pivoting a matrix using largest magnitude coefficent and diagonal values

10 次查看(过去 30 天)
I have a question for an assignment asking this
c. Write a function in MATLAB (rowpivot.m) that performs pivoting on a matrix (determines the location of the largest magnitude coefficient at or below the specified diagonal value and swaps the rows as necessary). The function will work for any location along the diagonal 1 through n). This function will call rowswap.m written in part (a) as necessary. Verify that the function works by pivoting on the original matrix in part (a) for the second element along the diagonal.
I did the other parts but i am alittle confused on what to do with this. The code I have for it is below
function Matrix_Pivot=rowpivot(Matrix,y)
D=diag(Matrix);
[a,b]=max(D(y,:));
Matrix_Pivot=rowswap(D,b,1);
end
would this be the correct way to get the diagonal value and swap the required rows.
Rest of the code is below if that helps
%this is the main script of the program
%Sets the matrix paramaters
Matrix=[1 7 -4 3 -3; 0 3 1 -2 2;0 4 3 4 5; 0 -5 8 1 3];
%Commands for rowswap
fprintf('The following matrix has had its rows swapped\n');
Matrix_Swap=rowswap(Matrix,2,3);
disp(Matrix_Swap);
%commands for row reduction
fprintf('The following matrix has had its rows reduxed\n')
Matrix_Redux=rowredux(Matrix,3,2,4/3);
disp(Matrix_Redux)
%Commands for row pivot
fprintf('The following matrix has had its rows pivoted\n')
Matrix_Pivot=rowpivot(Matrix,2);
disp(Matrix_Pivot)
rowswap
function Matrix_Swap=rowswap(Matrix,a,b)
Matrix_Swap=Matrix;
Matrix_Swap(a,:)=Matrix(b,:);
Matrix_Swap(b,:)=Matrix(a,:);
end
rowredux
function Matrix_Redux=rowredux(Matrix,c,d,e)
Matrix_Redux=Matrix;
Matrix_Redux(c,:)=Matrix(c,:)-Matrix(d,:)*e;
end

回答(1 个)

Arjun
Arjun 2025-6-2,9:54
I see that you want to perform row pivoting operation as described in the problem statement but you are unsure about the correctness of your code.
Let us analyse the provided code:
function Matrix_Pivot=rowpivot(Matrix,y)
D=diag(Matrix); % Returns a column vector
[a,b]=max(D(y,:)); % Finding max in a vector
Matrix_Pivot=rowswap(D,b,1); % Hardcoded swap with 1st row. Infact this operation is happening in a vector
end
Analysis:
  • diag(Matrix): returns a vector, not a matrix. "diag" in MATLAB returns a column vector containing all the diagonal elements. Refer to the documentation of "diag" here: https://www.mathworks.com/help/matlab/ref/diag.html
  • max(D(y,:)) : is essentially finding maximum element in a column. Also for largest magnitude coefficent, absolute value must be considered.
  • rowswap(D,b,1) : The row y should be swapped with the row having largest coefficent. You have hardcoded swap with the first row.
Kindly refer to the following code section below to address the potential issues with your code.
function Matrix_Pivot = rowpivot(Matrix, y)
% Get the number of rows
[n, ~] = size(Matrix);
% Extract the column from row y to n i.e. at or below the specified diagonal value
[~, maxIndex] = max(abs(Matrix(y:n, y)));
% Adjust index to match actual row in Matrix
maxRow = maxIndex + y - 1;
% Swap rows if needed
if maxRow ~= y
Matrix_Pivot = rowswap(Matrix, y, maxRow);
else
Matrix_Pivot = Matrix;
end
end
I hope this helps!

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by