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

2 次查看(过去 30 天)
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 个评论
Lois Slangen
Lois Slangen 2019-8-14
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.

请先登录,再进行评论。

采纳的回答

Andrey Kiselnikov
Andrey Kiselnikov 2019-8-14
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 个评论
Andrey Kiselnikov
Andrey Kiselnikov 2019-8-14
编辑:Andrey Kiselnikov 2019-8-14
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);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by