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 Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by