Finding the first index of a row where all integers of a defined list have occurred

조회 수: 3(최근 30일)
I have a 100x100 list values (1,2,3,4) selected at random.
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?

채택된 답변

John D'Errico
John D'Errico 2023년 1월 27일
Given a vector, can you simply find the first index where all have occurred? Just use ismember.
x = 1×100
4 1 1 4 2 4 2 4 1 1 4 1 4 4 4 4 4 3 4 3 4 4 4 4 3 4 1 2 4 1
[~,locs] = ismember(1:4,x)
locs = 1×4
2 5 18 1
ans = 18
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
Steve 2023년 1월 28일
Thanks. I see, the max index of the first occurance is the last number to appear, and therefore all number have appeared at this point. Nice solution. Cheers!

댓글을 달려면 로그인하십시오.

추가 답변(1개)

the cyclist
the cyclist 2023년 1월 27일
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;
% 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);
output = max(loc,[],2)
output = 100×1
5 5 5 5 12 15 12 8 6 5
Personally, I would use the loop, with John's solution.


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!

Translated by