How do I store row swap information in a vector?

5 views (last 30 days)
Suppose I have 2 matrices A and B.. A is an NxN matrix and B is an Nx1 matrix. Now, I want to swap certain rows in A and store the information in an array P and use the array P to swap the rows in B. Is there a way to do this?
  2 Comments
Image Analyst
Image Analyst on 22 Oct 2012
I don't know what P is - is it A with "certain rows" swapped? What does "use the array P to swap the rows in B" mean. How, given only that P matrix, are you supposed to know what to do with B? If you know which rows in A were swapped and you wanted to swap the same elements in B, you wouldn't even need P at all. Please give a small example (say N = 4 or 5) showing A, P, and B.
Sandeep
Sandeep on 23 Oct 2012
Ok, let me give an example.. Suppose I want to solve Ax=B using gaussian elimination with partial pivoting in 2 steps. First, I want to apply LU decomposition on A and then solve LUx=B. Now, I want to create 2 functions, one to solve LU decomposition and another one to solve LUx=B. The first function needs to store L and U in a single vector and create a P vector which stores the swap information. The second function takes L and U in a single matrix, P and B as input and then solves for x. My question is how to store the row swap information in P in function 1 and how to implement it in the second function on B.

Sign in to comment.

Answers (1)

Sachin Ganjare
Sachin Ganjare on 22 Oct 2012
Command for swapping array:
For 3x3 Matrix A:
A([1 3], :) = A([3 1], :)
This will swap row 1 with 3.
Now you can use for loop for swapping the rows, at the same time saving swapped rows information in matrix P.
Then you run another loop for swapping rows in matrix B, but this time with swapping information present in matrix P.
Hope this helps!!!

Categories

Find more on Linear Algebra 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!