How to obtain the original matrix after performing symrcm or symamd in MATLAB?

7 views (last 30 days)
I use the Cuthill-McKee algorithm. After finding the desired vector, I need to restore the original matrix. I found the answer here https://www.mathworks.com/matlabcentral/answers/94946-how-do-i-obtain-the-original-mapping-after-performing-symrcm-in-matlab-7-1-r14sp3 but for my images I get "Index exceeds matrix dimensions". Is there any other way to do this? (Preferably without the availability of the original matrix)
symV %original matrix
k1 = symrcm(symV)
matrix_after_cm = symV(k1,k1);
spy(first_after_cm)

Accepted Answer

Fabio Freschi
Fabio Freschi on 9 Oct 2019
Can you share the symV original matrix?
  2 Comments
Kadyr Sherfedinov
Kadyr Sherfedinov on 9 Oct 2019
fullFileName = '/Users/user/Desktop/binary2.png';
rgbImage = imread(fullFileName);
binaryImage = ~im2bw(rgbImage, 0.5);%converting to binary image
v = triu(binaryImage);%getting upper triangular matrix
n = tril(binaryImage);%getting lower triangular matrix
symV = (v+v') - eye(size(v,1)).*diag(v)%diagonal reflection of the upper triangular matrix (obtaining a symmetric matrix)
symN = (n+n') - eye(size(n,1)).*diag(n)%diagonal reflection of the lower triangular matrix (obtaining a symmetric matrix)
k1 = symrcm(symV)
first_after_cm = symV(k1,k1);
spy(first_after_cm)
binary2.png
Fabio Freschi
Fabio Freschi on 9 Oct 2019
I run your code, then I run
[~,q] = sort(k1);
AfromB = first_after_cm(q,q); % Elapsed time is 0.002503 seconds.
norm(AfromB-symV) % ans = 0
without any problem. Note that your matrix is really sparse, so you can benefit handling it as sparse matrix. Can you try this
symV = sparse(symV);
before symrcm? It is not as smart as directly assembling the matrix as sparse, but it should work.

Sign in to comment.

More Answers (1)

Fabio Freschi
Fabio Freschi on 8 Oct 2019
Try this:
% matrix dimensions
N = 5;
% create symmetric matrix
A = rand(N);
A = A+A.';
% symrcm sorting
p = symrcm(A);
% sorted matrix
B = A(p,p);
% inverse sorting
[~,q] = sort(p)
% re-create A matrix
AfromB = B(q,q);
% chech
norm(A-AfromB);
% ans =
%
% 0
  1 Comment
Kadyr Sherfedinov
Kadyr Sherfedinov on 9 Oct 2019
Edited: Kadyr Sherfedinov on 9 Oct 2019
This works for matrix of dimension 5. I'm using real image(converted to binary and symmetry) with 1172x1172 size. And i get "Requested 1373584x1373584 (14057.3GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become unresponsive." exception in this line of code
AfromB = B(q,q);

Sign in to comment.

Categories

Find more on Sparse Matrices 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!