5 views (last 30 days)

Show older comments

Good Afternoon,

I have a matrix, M, containing 29 rows and 92 columns of data. The matrix contains numeric values ranging from 1 to 8. I would like to be able to compare each column of the matrix, M, with a column vector, V and identify which columns of the matrix M are most similar to the vector V.

For example if the first element in vector V is 1, then a similar column in matrix M would contain the value 1 in the first row. Likewise if the second element in V is 6, then a similar result in M would contain 6. The goal is to identify the column(s) of M most similar to the vector V and if possible, rank the columns from most similar to least similar.

Had the matrix been smaller I would have been able to do this by hand, however I wondered if there was a way to automate this process in MATLAB to save time and possibly handle larger data sets.

Vineet Joshi
on 22 Mar 2021

As per my understanding of the question, you aim to automate the process of sorting the columns of a matrix based on their similarity with a column vector, where similarity is defined by comparing the sequences of elements.

This can be done as follows:

- Check for exact element wise match of the column with the reference vector.
- If there is no exact match from the above, shift the array elements using circshift and check for element wise match.

You can associate a value with each column to help you in sorting.

You can refer the following code for reference.

%Sample Data Matrix

Data = [1,2,3,4,5;0,2,3,4,5;2,3,4,5,0;7,3,4,5,0;3,4,5,1,2;7,8,9,10,11]';

%Reference Vector

A = [1,2,3,4,5]';

%Loop through each column in the matrix.

for i=1:6

%Compare elementwise matching score between vector i and reference

%vector.

Value = sum(Data(:,i) == A);

%If no elemnts match. Try circular shift operation to find matching

%sequences.

if Value == 0

%In case two sequences exits, we need to find the maximum one.

max_val = 0;

%Apply the operation until vector is back to original.

for j=1:5

%Rotate the vector by j elements.

Rotated_vec = circshift(Data(:,i),j);

%Check for maximum elemntwise match.

max_val = max(max_val,sum(Rotated_vec == A));

end

%Subtract a small amount as a penality for rotation.

Value = max_val - 1;

end

fprintf('Column = %d, Value = %d\n',i,Value)

end

%Output

Column = 1, Value = 5

Column = 2, Value = 4

Column = 3, Value = 3

Column = 4, Value = 2

Column = 5, Value = 4

Column = 6, Value = -1

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

Start Hunting!