Logical Indexing one vector using a second vector

7 ビュー (過去 30 日間)
Dritan Nikolla
Dritan Nikolla 2017 年 6 月 6 日
コメント済み: Dritan Nikolla 2017 年 6 月 6 日
Hi,
I am relatively new to MatLab. I have the following problem. I have a double array in my data named Trial_Index size 50478x1 I also have a myTrialNumbers double array size 154x1.
The Trial_Index array has data in the following form:
Trial_Index = [1 1 1 1 2 2 3 3 3 3 4 5 5 .... n]
The TrialNumbers has data in the following form:
TrialNumbers = [1 2 3 4 5 ....... n]
I need a way to automate the task of generating one seperate logical vector for each element of TrialNumbers where it is found in TrialIndex. So that:
Vector1 = TrialNumbers(1) == Trial_Index If TrialNumbers(1) equals 1 then the Vector1 should contain:
Vector1 = [1 1 1 1 0 0..... 0]
If TrialNumbers(2) equals 2 (the second element in TrialNumbers), then Vector2 shouod containt:
Vector2 = [0 0 0 0 1 1 0 0 ... 0].
It looks simple, I cannot find an answer to this.
Please help.

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 6 月 6 日
Vectors = bcxfun(@eq,Trial_Index(:),TrialNumbers(:).');
  2 件のコメント
Dritan Nikolla
Dritan Nikolla 2017 年 6 月 6 日
Hi Bobrov,
How does this work? I am getting a:
Undefined function or variable 'bcxfun'.
Error in mycellfind (line 47)
Vectors = bcxfun(@eq,TRIAL_INDEX(:),myTrialCodes(:).');
Dritan Nikolla
Dritan Nikolla 2017 年 6 月 6 日
You are amazing :), it works, there is a spelling error. it should be:
Vectors = bsxfun(@eq,Trial_Index(:),TrialNumbers(:).');

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by