Related to finding columns of a matrix satisfying specific conditions

Hello all,consider the 8 X 4 matrix whose columns are shown below: (Note: Actually we have a large matrix of dimension 8 X 500, but for simplicity we are considering 8 X 4 matrix).
My query is how to find the columns in which only the 3rd and 5th row are non-zero while all other rows are zero.
Any help in this regard will be highly appreciated.
% Col 1
0.00000000000000 + 0.00000000000000i
1.50731573207606 + 0.0126629716692995i
0.00000000000000 + 0.00000000000000i
0.00000000000000 + 0.00000000000000i
0.00000000000000 + 0.00000000000000i
0.00000000000000 + 0.00000000000000i
0.00000000000000 + 0.00000000000000i
1.05524235130179 - 1.06946690410098i
% Col 2
0.00000000000000 + 0.00000000000000i
0.00000000000000 + 0.00000000000000i
0.570954576337680 - 0.0694208400277470i
0.00000000000000 + 0.00000000000000i
-0.439792062677585 + 0.906860087559601i
0.00000000000000 + 0.00000000000000i
0.00000000000000 + 0.00000000000000i
0.00000000000000 + 0.00000000000000i
% Col 3
0.00000000000000 + 0.00000000000000i
0.00000000000000 + 0.00000000000000i
-0.803157531649936 - 0.535481484911063i
0.00000000000000 + 0.00000000000000i
-0.669680856264729 + 0.552075228739879i
0.00000000000000 + 0.00000000000000i
0.00000000000000 + 0.00000000000000i
0.00000000000000 + 0.00000000000000i
% Col 4
-0.208494084667111 - 0.237272112154493i
0.00000000000000 + 0.00000000000000i
0.00000000000000 + 0.00000000000000i
0.00000000000000 + 0.00000000000000i
0.00000000000000 + 0.00000000000000i
0.00000000000000 + 0.00000000000000i
-0.117364925574855 + 0.139800044597261i
0.00000000000000 + 0.00000000000000i

 Accepted Answer

nz = YourMatrix ~= 0;
pattern = [false; false; true; false; true; false; false; false];
matching_column_mask = all(nz == pattern, 1);
You can use matching_column_mask fairly directly, but if you really need to you can find() on it.
Note that I interpreted "3rd and 5th row are non-zero" to mean that they must be non-zero, rather than that they are permitted to be non-zero.
If the rule were that those rows are permitted to be non-zero then
matching_column_mask = all(YourMatrix([1 2 4 6 7 8],:) == 0,1);

4 Comments

Thank you so much sir for your response...Everytime its not necessary that 3rd and 5th row will only be non-zero...Any two rows can be non-zero...Then in that case above program will not work...
At any given time do you know which specific rows must be non-zero, or is it that exactly two rows must have non-zeros?
A = [ 0 0 0 6 0
1 0 0 7 10
0 0 2 8 0
3 0 5 9 11
]
We can see that the second column should not match as it has no non-zeros at all.
We can see that the 4th column should not match as it has more than two rows with non-zeros.
Now, column 1 has exactly two non-zero rows, so we should consider it as a potential candidate.
column 3 also has exactly two non-zero rows, so we should consider it as a potential candidate too. But it has a different set of non-zero rows than column 1 is -- so should we discover both column 1 and column 3, or should we say that the pattern should not be considered to be followed unless there are at least two columns with the same set of two non-zero rows? Such as column 5, which has the same pattern as column 1 -- so should we match columns 1 and 5 and say column 3 does not qualify? But clearly in the general case we could also happen to have another column that happened to match the pattern for column 3, so what do we do then when there is a tie?
Therefore, analyzing an array to "discover" the pattern can result in ambiguity over what should be returned. This ambiguity is not present if at any given time you know which two rows are to be the ones matched.
rows_to_match = [3, 5];
nz = YourMatrix ~= 0;
pattern = false(height(YourMatrix),1);
pattern(rows_to_match) = true;
matching_column_mask = all(nz == pattern, 1);
The above should work provided that at the time of execution you know which two rows you want to check.
Thank u so much sir for your response....

Sign in to comment.

More Answers (1)

% Generate a test matrix
X = rand(8, 500);
X(:,10) = [0 0 3 0 5 0 0 0];
X(:,20) = [0 0 0 0 5 0 0 0]; % won't be detected since X(3,20) is zeros
mask = true(size(X,1),1);
mask([3,5]) = false;
find(all(xor(mask, logical(X)), 1))
ans = 10

Community Treasure Hunt

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

Start Hunting!