Matrix Manipulation Question

Given a 1 * N matrix or an array, how do I find the first 4 elements that have the same value, and then save the index for these elements?

PS: I'm just curious. What if we want to find the first 4 elements whose values ​​are in a certain range, say, below 2? For example, M = [10,15,14.5,9,15.1,8.5,15.5,9.5], the elements I'm looking for will be 15,14.5,15.1,15.5, and the indices will be 2,3,5,7.

+5
source share
6 answers

If you want the first value to be present 4 times in the array tab in Matlab, you can use

num_min = 4
val=NaN;
for i = tab
    if sum(tab==i) >= num_min
        val = i;
        break
    end
end
ind = find(tab==val, num_min);

For example, using

tab = [2 4 4 5 4 6 4 5 5 4 6 9 5 5]

You get

val =
     4
ind =
     2     3     5     7
+7
source

MATLAB:

array = randi(5, [1 10]);            %# random array of integers

n = unique(array)';                  %'# unique elements
[r,~] = find(cumsum(bsxfun(@eq,array,n),2) == 4, 1, 'first');
if isempty(r)
    val = []; ind = [];              %# no answer
else
    val = n(r);                      %# the value found
    ind = find(array == val, 4);     %# indices of elements corresponding to val
end

:

array =
     1     5     3     3     1     5     4     2     3     3
val =
     3
ind =
     3     4     9    10

:

, . :

n =
     1
     2
     3
     4
     5

, BSXFUN, , . :

result = zeros(length(n),length(array));
for i=1:length(n)
    result(i,:) = (array == n(i));        %# row-by-row
end

, :

result =
     1     0     0     0     1     0     0     0     0     0
     0     0     0     0     0     0     0     1     0     0
     0     0     1     1     0     0     0     0     1     1
     0     0     0     0     0     0     1     0     0     0
     0     1     0     0     0     1     0     0     0     0

CUMSUM result . , :

>> cumsum(result,2)
ans =
     1     1     1     1     2     2     2     2     2     2
     0     0     0     0     0     0     0     1     1     1
     0     0     1     2     2     2     2     2     3     4
     0     0     0     0     0     0     1     1     1     1
     0     1     1     1     1     2     2     2     2     2

cumsum(result,2)==4 ( , , ):

>> cumsum(result,2)==4
ans =
     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     1
     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0

, FIND 1 : , 1 , . (r=3), val = n(r). , , 4 , , , 1, .

- FIND...

+2

++

std::map<int,std::vector<int> > dict;

std::vector<int> ans(4);//here we will store indexes
bool noanswer=true;

//my_vector is a vector, which we must analize
for(int i=0;i<my_vector.size();++i)
{
    std::vector<int> &temp = dict[my_vector[i]];
    temp.push_back(i);
    if(temp.size()==4)//we find ans
    {
         std::copy(temp.begin(),temp.end(),ans.begin() );
         noanswer = false;
         break;
    }   
}
if(noanswer)
   std::cout<<"No Answer!"<<std::endl;
0

Amro.,

Matlab. , . , 4 , .

tab = [2 5 4 5 4 6 4 5 5 4 6 9 5 5]

%this is a loop to find the indicies of groups of 4 identical elements
tot = zeros(size(tab));
for nn = 1:numel(tab)
    idxs=find(tab == tab(nn), 4, 'first');
    if numel(idxs)<4
        tot(nn) = Inf;
    else
        tot(nn) = sum(idxs);
    end        
end

%find the first 4 identical
bestTot = find(tot == min(tot), 1, 'first' );

%store the indicies you are interested in.
indiciesOfInterst = find(tab == tab(bestTot), 4, 'first')
0

, :

l = 10; m = 5; array = randi(m, [1 l])

A = zeros(l,m);    % m is the maximum value (may) in array
A(sub2ind([l,m],1:l,array)) = 1;
s = sum(A,1);
b = find(s(array) == 4,1); 
% now in b is the index of the first element

if (~isempty(b)) 
    find(array == array(b))
else 
    disp('nothing found'); 
end

, . '1' , - () (). . : , A .

0

You PS question is more complicated. I did not have time to check each case, but the idea is here:

M=[10,15,14.5,9,15.1,8.5,15.5,9.5]
val = NaN;
num_min = 4;
delta = 2;
[Ms, iMs] = sort(M);
dMs = diff(Ms);
ind_min=Inf;
n = 0;
for i = 1:length(dMs)
    if dMs(i) <= delta
    n=n+1;
    else
    n=0;
    end
    if n == (num_min-1)
        if (iMs(i) < ind_min)
            ind_min = iMs(i);
        end
    end
end
ind = sort(iMs(ind_min + (0:num_min-1)))
val = M(ind)
0
source

All Articles