Finding the first index of a row where all integers of a defined list have occurred
5 views (last 30 days)
Show older comments
I have a 100x100 list values (1,2,3,4) selected at random.
x=randi([1,4],100,100);
Reading left to right, I want to find the first index in each row where all the numbers have appeared (1,2,3,4), irrespecitve of order or repeats.
For example, in the random data (4,2,3,2,4,4,1,1,2,4,2,3...), all numbers have appeared at index 7.
Does this require a loop?
0 Comments
Accepted Answer
John D'Errico
on 27 Jan 2023
Given a vector, can you simply find the first index where all have occurred? Just use ismember.
x=randi([1,4],1,100)
[~,locs] = ismember(1:4,x)
max(locs)
So max(locs) is the index at which point ALL of those elements have been seen. No loop was needed for that part. You could now just use a loop on the rows of a matrix with multiple rows.
Can you do this without a loop? Well, yes. The way to do it would require tools like mat2cell, and then cellfun. That is, you could convert the array to a cell array of vectors. Then use cellfun to apply the ismember operation (written as an m-file) to each vector.
Is it worth it, to write a code that you don't really understand, and will do what you want no more efficiently than a simple loop? A complicated looking line of code is not better than a simple loop. Remember, you need to understand that code in order to debug and maintain it in the future.
3 Comments
John D'Errico
on 28 Jan 2023
Edited: John D'Errico
on 28 Jan 2023
Lol. No problem. There are probably other ways I could have done it too, but lets take it apart on an example vector.
X = [2 1 2 5 1 5 4 3 2 1];
So the first occurrence of a 1 is the second element. Right? 2 happens at the first element. 3 happens where? The 8th element. Finally, where does the 4 first happen? At element 7.
Now, lets read the help for ismember.
help ismember
What does the second output argument tell us? It indcates the first occurrence of each element. So we have
[~,locs] = ismember([1 2 3 4],X)
Is that not exactly where I said those elements first appear?
Now, what happens when you use max on a vector? It tells you the LARGEST element in the vector.
max(locs)
Now go back and think about the goal here. We want to know at what point ALL of the elements in [1 2 3 4] have been found in the original vector X. No sooner, no later.
So max(locs) yields exactly what you wanted. Does it work only for a vector? Well yes. But as has been said, there is nothing wrong with a loop over the rows of your matrix. Apply ismember as I have just done for each row of your array.
Loops are not overtly inefficient. Yes, avoid double and more deeply nested loops, if you can. But good code wants to be readable code. And you need to maintain your code. If you can easily read that code, then that is important. With experience the code we write tends to have fewer loops. But only when a loop is seen to be a significant bottleneck to performance should you be worried about those loops.
More Answers (1)
the cyclist
on 27 Jan 2023
I 100% agree with @John D'Errico's take on this, which is that any non-loop solution here is (probably) going to be sufficiently obfuscated that "future you" will regret avoiding the loops.
One reason is that there is no "natural" way to avoid loops is that find and ismember don't really have "row-wise" implementations (to my knowledge) that do what you are trying to do. (There is a bit of discussion of this in the forum.)
Here is an example of an obfuscated solution:
% The input
rng default
N = 4;
x=randi([1,N],100,100);
% The algorithm
% (Technically, I supposed this is a for loop, but not over your rows or columns)
loc = zeros(height(x),N);
for val = 1:N
[~,loc(:,val)] = max(x==val,[],2);
end
output = max(loc,[],2)
Personally, I would use the loop, with John's solution.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements 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!