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

조회 수: 1 (최근 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);
Andrei Bobrov
Andrei Bobrov 2019년 8월 14일
indicator = ismember(timeline,...
[specific_time_points1(:);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