Clear Filters
Clear Filters

Randperm in symmetric matrix

5 views (last 30 days)
If i have a symmetric matrix and i want to select a random entries from this matrix and let these random entries belong to a set omega.. I can do that by use a randperm function however since the matrix is symmetric, I want to select the same corresponding entries.. so for example if we have 4×4 matrix and in randperm select the indices 2 6 8 and set this as omega then i want to update omega to include also the symmetric position such that 5 6 14

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 3 Apr 2023
%Let y be the matrix
y=rand(4);
s=size(y);
%Indices
x=[2 6 8];
[r,c]=ind2sub(s,x);
%Interchanging row and column
z=sub2ind(s,c,r)
z = 1×3
5 6 14
%Include the indices
x=[x z]
x = 1×6
2 6 8 5 6 14

More Answers (2)

Jack
Jack on 3 Apr 2023
To select random entries from a symmetric matrix and ensure that the corresponding symmetric positions are also included in the selected set, you can do the following:
  1. Use the randperm function to generate a random set of indices omega in the lower triangle of the matrix.
  2. Generate the corresponding indices in the upper triangle by adding the number of columns to each index. For example, if omega = [2 6 8], then the corresponding upper triangle indices would be omega + 4 = [6 10 12].
  3. Concatenate the two sets of indices to obtain the complete set of indices omega, including both the lower and upper triangle.
Here's an example code snippet that implements these steps:
% Generate a random symmetric matrix
n = 4;
A = randn(n);
A = A + A.'; % make A symmetric
% Generate a random set of indices in the lower triangle
n_lower = n*(n-1)/2;
omega_lower = randperm(n_lower, 3);
% Generate the corresponding indices in the upper triangle
omega_upper = omega_lower + n;
% Concatenate the two sets of indices to obtain the complete set
omega = sort([omega_lower omega_upper]);
% Display the selected indices and their corresponding symmetric positions
fprintf('Selected indices:\n');
disp(omega(:));
fprintf('Corresponding symmetric positions:\n');
disp(omega(:) + n*(ceil(omega(:)/n) - 1) - (n*(n-1)/2)));
This code generates a random 4x4 symmetric matrix A and selects a random set of 3 indices in the lower triangle using randperm. It then computes the corresponding indices in the upper triangle and concatenates the two sets of indices to obtain the complete set omega. Finally, it displays the selected indices and their corresponding symmetric positions in the matrix.
Note that in the last line of the code, the symmetric positions are computed using a formula that maps each index to its corresponding row and column in the lower triangle of the matrix, and then maps that position to its corresponding index in the upper triangle. This formula assumes that the matrix is square (i.e., n is even) and that the indices in omega are sorted in ascending order. If your matrix has a different size or the indices are not sorted, you may need to adjust the formula accordingly.
  5 Comments
Walter Roberson
Walter Roberson on 3 Apr 2023
  • Generate the corresponding indices in the upper triangle by adding the number of columns to each index. For example, if omega = [2 6 8], then the corresponding upper triangle indices would be omega + 4 = [6 10 12].
No, in a 4 x 4 array, adding 4 to a linear index gets you the index of the same row in the next column.
[r, c] = ind2sub([4 4], LINEARINDICES);
SYMLINEARINDICES = sub2ind([4 4], c, r)
Joss Knight
Joss Knight on 4 Apr 2023
I think user Jack is a chat bot so probably that's why it doesn't work.

Sign in to comment.


Walter Roberson
Walter Roberson on 3 Apr 2023
Moved: Walter Roberson on 3 Apr 2023
You can build a mapping:
LINEARINDICES = randperm(16,3)
LINEARINDICES = 1×3
9 6 13
MAPPING = reshape(reshape(1:16,4,4).', 1, []);
SYMLINEARINDICES = MAPPING(LINEARINDICES)
SYMLINEARINDICES = 1×3
3 6 4
%verification
[r, c] = ind2sub([4 4], LINEARINDICES);
SYMLINEARINDICES2 = sub2ind([4 4], c, r)
SYMLINEARINDICES2 = 1×3
3 6 4

Categories

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