Reputation: 111
I need to loop through coloumn 1 of a matrix and return (i) when I have come across ALL of the elements of another vector which i can predefine.
check_vector = [1:43] %% I dont actually need to predefine this - i know I am looking for the numbers 1 to 43.
matrix_a coloumn 1 (which is the only coloumn i am interested in looks like this for example
1 4 3 5 6 7 8 9 10 11 12 13 14 16 15 18 17 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 1 3 4 2 6 7 8
We want to loop through matrix_a and return the value of (i) when we have hit all of the numbers in the range 1 to 43. In the above example we are looking for all the numbers from 1 to 43 and the iteration will end round about position 47 in matrix_a because it is at this point that we hit number '2' which is the last number to complete all numbers in the sequence 1 to 43. It doesnt matter if we hit several of one number on the way, we count all those - we just want to know when we have reached all the numbers from the check vector or in this example in the sequence 1 to 43.
Ive tried something like:
completed = []
for i = 1:43
complete(i) = find(matrix_a(:,1) == i,1,'first')
end
but not working.
Upvotes: 2
Views: 171
Reputation: 160
Here is one solution without a loop and without any conditions on the vectors to be compared. Given two vectors a
and b
, this code will find the smallest index idx
where a(1:idx)
contains all elements of b
. idx
will be 0 when b
is not contained in a
.
a = [ 1 4 3 5 6 7 8 9 10 11 12 13 14 16 15 18 17 19 20 21 22 23 24 25 26 ...
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 1 3 4 2 6 7 8 50];
b = 1:43;
[~, Loca] = ismember(b,a);
idx = max(Loca) * all(Loca);
Some details:
ismember(b,a)
checks if all elements of b
can be found in a
and the output Loca
lists the indices of these elements within a
. The index will be 0, if the element cannot be found in a
.idx = max(Loca)
then is the highest index in this list of indices, so the smallest one where all elements of b
are found within a(1:idx)
.all(Loca)
finally checks if all indices in Loca
are nonzero, i.e. if all elements of b
have been found in a
.Upvotes: 0
Reputation: 112659
To find the first entry at which all unique values of matrix_a
have already appeared (that is, if check_vector
consists of all unique values of matrix_a
): the unique
function almost gives the answer:
[~, ind] = unique(matrix_a, 'first');
result = max(ind);
Upvotes: 1
Reputation: 221514
Assuming A
as the input column vector, two approaches could be suggested here.
Approach #1
With arrayfun
-
check_vector = [1:43]
idx = find(arrayfun(@(n) all(ismember(check_vector,A(1:n))),1:numel(A)),1)+1
gives -
idx =
47
Approach #2
With customary bsxfun
-
check_vector = [1:43]
idx = find(all(cumsum(bsxfun(@eq,A(:),check_vector),1)~=0,2),1)+1
Upvotes: 2
Reputation: 5672
Someone might have a more compact answer but is this what your after?
maxIndex = 0;
for ii=1:length(a)
[f,index] = ismember(ii,a);
maxIndex=max(maxIndex,max(index));
end
maxIndex
Upvotes: 0