finding al points of a vector in my array and assigning them a different number.

1 vue (au cours des 30 derniers jours)
Hi, I have specific timespoints along my timeline that I want to assign a diiferent number. So let's say I have a timeline from 1 to 2000 and I have a vector with a different amount of time points that are somewhere on my timeline (so between 1 and 2000). I want to create a new vector, called indicator, where all the specific time points are assigned a value of 1 and where all the other time points in my timeline are assigned 0. Here is what I have:
counter = 0;
for ii = 1:length(timeline)
if ii == specific_time_points
counter = counter + 1;
indicator (counter) = ii;
indicator = 1;
else indicator = 0;
end
end
But, 'indicator' now only has one value of 0, instead I want it to give a vector of length 2000 with 1's were my specific_time_points are and zeros for the rest of my timeline.
I don't know what I am doing wrong, I hope someone can help me.
  1 commentaire
Lois Slangen
Lois Slangen le 14 Août 2019
I should mention however that I have two different vectors, lets say specific_time_points1 and specific_time_points2 that have the same timeline. And my new vector indicator should contain a 1 where specific_time_points1 and/or specific_time_points2 are in the timeline and zero every where else.

Connectez-vous pour commenter.

Réponse acceptée

Andrey Kiselnikov
Andrey Kiselnikov le 14 Août 2019
Hi here it is
a = 1:1:10;
b = [1 3 5];
>> ismember(a,b)
ans =
1×10 logical array
1 0 1 0 1 0 0 0 0 0
If my help was useful please mark answer as accepted!
  5 commentaires
Andrey Kiselnikov
Andrey Kiselnikov le 14 Août 2019
Modifié(e) : Andrey Kiselnikov le 14 Août 2019
indicator_1 = ismember(timeline,specific_time_points1);
indicator_2 = ismember(timeline,specific_time_points2);
indicator = indicator_1 | indicator_2;
it was for better understanding, in one line it looks like
indicator = ismember(timeline,specific_time_points1) | ismember(timeline,specific_time_points2);
Andrei Bobrov
Andrei Bobrov le 14 Août 2019
indicator = ismember(timeline,...
[specific_time_points1(:);specific_time_points2(:)]);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by