Making an Array out of another Array if conditions are met

79 ビュー (過去 30 日間)
Rachael Bell
Rachael Bell 2022 年 6 月 7 日
編集済み: Rachael Bell 2022 年 6 月 7 日
Hello!
So I have a double array that is 15369x9. I am attempting to make a raster plot from this but inorder to do so i need to break the raw data array down into two different arrays, specifically one that has all of the trial starts in it( indicated by a number 1 in the 5th column) and licks(indicated by a 0 in the 5th column) I know I need to use a for loop to index through the array and I want to use an if loop to generate the new array, but I have no real idea how to do that. Any help would be greatly appreciated. Thanks!

採用された回答

James Tursa
James Tursa 2022 年 6 月 7 日
編集済み: James Tursa 2022 年 6 月 7 日
You don't need a for-loop for this. The best way is to use logical indexing. See this link:
Basically, you create a logical vector with the conditions you want, and then use that to index into the variable. E.g.,
M = your matrix
M(:,5) is the 5th column of the matrix
x = M(:,5)==1 is a logical vector indicating where there is a 1 in the 5th column
M(x,:) is a subset of M where there is a 1 in column 5.
Etc.
  2 件のコメント
Rachael Bell
Rachael Bell 2022 年 6 月 7 日
編集済み: Rachael Bell 2022 年 6 月 7 日
My data is currently in array form, can i still do that or do i need to do some kind of conversion? Also I forgot to mention it in my main post but I do need to keep at the first column information( timestamps) and the sixth column information( Trial Number) It seems like if I used logical operators I will loose my trial number
James Tursa
James Tursa 2022 年 6 月 7 日
The method shown retains the entire rows where the column data matches your desired value. I did make an edit to my original post so be sure to grab the latest code shown.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by