MATLAB Answers

0

How can I exclude index values that are not present in all columns of a matrix?

Asked by Kelly Thompson on 4 May 2019
Latest activity Answered by Cris LaPierre
on 16 May 2019
so I have this 900x23 matrix of linear index values, in which each column (1-23) is a vector of index values for nonzero values for a different matrix. Each of the columns might have a different number of index values, and the rest of the matrix underneath is filled up with zeros so the number of rows is the same across all 23 matrices. Here is the code for how I produced this matrix:
% indAcute will be the 900x23 matrix of index values
indAcute = [];
%upperAcute is a 90x90x23 matrix, so this for loop runs from 1:23, with each matrix being a 90x90
for i = 1:size(upperAcute,3)
%finds the nonzero values of each matrix, creates a vector of the linear index values in ind
ind = find(upperAcute(:,:,i));
%creates a vector of zeros the size of 900 - the number of index values in ind
zer = zeros((900-size(ind,1)),1);
%creates a vector that is 900 elements long, containing index values followed by zeros
indLong = [ind; zer];
%concatenates the vector of indices with the existing indAcute matrix
indAcute = [indAcute, indLong];
end
what I want to do is end up with an Nx23 matrix, where it only includes index values that are present in ALL of the columns. If there's an index value that is missing in one of them, I want to remove it (or replace it with a zero). I was thinking maybe the ismember() function might be useful, but i'm unsure of how to structure my code. Any advice or tips would be greatly appreciated- thanks!

  2 Comments

Make it EASY for us to help you, not hard. Please attach a sample "upperAcute" matrix in a .mat file with the paper clip icon so we can help you with your actual data.

Sign in to comment.

1 Answer

Answer by Cris LaPierre
on 16 May 2019

Where your data is sorted in ascending order and, at least after a cursory examination of the data, no values are duplicated in a column (excluding the zeros at the bottom), the first thing to realize is that your final indices will be the same for every column. You can find what this final set of indices are by using the instersect fuction as you loop through each column.
intInd = indAcute(indAcute(:,1)>0,1); % Find all nonzero values in the first column
for loop = 2:size(indAcute,2)
% Find intersect with current column
intInd = intersect(intInd,indAcute(:,loop));
end
This results in an 820x1 vector.

  0 Comments

Sign in to comment.