A = N-by-2 array, wherein A(:,1)<A(:,2)
B = M-by-2 array, wherein B(:,1)<B(:,2)
A and B are essentially time stamps.
C = unique(vertcat(A,B));
[~,D] = ismember(A,C);
[~,E] = ismember(B,C);
C([D(1,1):D(1,2),...D(N,1):D(N,2)],2) = any scalar, or an array with a sufficient number of elements to dstribute.
C([E(1,1):E(1,2),...E(N,1):E(N,2)],3) = any scalar, or an array with a sufficient number of elements to dstribute.
I'm hung up on defining those assignment indecis [D(1,1):D(1,2),...D(N,1):D(N,2)] without using a loop, i.e.
IdcsD = double.empty;
for i=1:size(D,1)
Idcs = [Idcs,[D(i,1):D(i,2)]];
end
C(IdcsD) = Value;

3 commentaires

Matt J
Matt J le 23 Nov 2022
What is the size of D and what is the size of 'Value'?
Matt J
Matt J le 23 Nov 2022
Please show an example for small N and showing the desired output D.
Gabriel Stanley
Gabriel Stanley le 23 Nov 2022
Modifié(e) : Gabriel Stanley le 23 Nov 2022
Value is a scalar, while OBE

Connectez-vous pour commenter.

 Réponse acceptée

Matt J
Matt J le 23 Nov 2022
Modifié(e) : Matt J le 23 Nov 2022
The question has nothing to do really with ismember. It's more about how to turn a list of interval end points into indices. One way is as follows:
D=[1 5 9;
2 7 12]' %interval end points
D = 3×2
1 2 5 7 9 12
C=rand(1,12);
nc=length(C);
assert(all(diff(D,1,2)>=0) & all(D(:)>0) & all(D(:)<=nc) ,'Invalid data present')
D=D+[0,1]; s=0*D+[1,-1];
csum=cumsum( accumarray(D(:),s(:),[nc+1,1]) );
Idcs=logical(csum(1:end-1))',
Idcs = 1×12 logical array
1 1 0 0 1 1 1 0 1 1 1 1
Idcs=find(Idcs) %Shouldn't be needed. Use logical indices above.
Idcs = 1×9
1 2 5 6 7 9 10 11 12

5 commentaires

Gabriel Stanley
Gabriel Stanley le 23 Nov 2022
I didn't think that my initial (or revised) question implied that I was asking about ismember functionality, only that I was using ismember in my process. That said, your answer is exactly what I was looking for. Thank you.
Follow-up coming back to what seems to be an improved version of this: why have the
C = rand(1,12);
nc = length(C);
instead of just
nD = max(D,[],'all');
?
Matt J
Matt J le 8 Mai 2023
I see no reason to assume nC and nD will be related in general, even though they were in the above example.
Gabriel Stanley
Gabriel Stanley le 8 Mai 2023
So in the case of the interval end points of D defining a subset of some larger data set C, nC should be set to the maximum index of the relevant dimension in C?
Matt J
Matt J le 8 Mai 2023
The dimension/shape of C seems irrelevant to this task. I think you would just flatten C to C(:) and apply the code above as is.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2019b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by