Splitting array based on a single column's condition

13 ビュー (過去 30 日間)
yz
yz 2017 年 12 月 19 日
コメント済み: Star Strider 2018 年 1 月 3 日
Hello, I am trying to separate my large array of [n rows, 5 columns] into individual arrays of [k rows, 5 columns] (where k < n). I want to split my array based on numbers on my fourth column. The fourth column has numbers that increase and decrease in a loop to create some peaks (example: goes from 0 to 35, then from 35 goes to 0, then from 0 to -35, etc.).
I've attached a sample code to help better explain what I mean.
Is there an efficient way to split these data? (So far I've been going into the data selecting where I want to split them up, and doing it manually.)
Thank you in advance.

採用された回答

Ramnarayan Krishnamurthy
Ramnarayan Krishnamurthy 2017 年 12 月 28 日
編集済み: Ramnarayan Krishnamurthy 2017 年 12 月 28 日
If the transition point in the fourth column is known to be 0, then you can consider splitting the data by the location of these 0's.
% Locate the zeros in the 4th column
index_0 = find(s_P1_5(:,4)==0);
% Include the start and end indices
idx = [0 index_0' size(s_P1_5,1)];
% Create individual cells containing the sub matrices
out = mat2cell(s_P1_5, diff(idx))
The split arrays will now be stored in out{1}, out{2} and so on.
  1 件のコメント
yz
yz 2018 年 1 月 3 日
This is awesome, thank you! Though, I'm trying to modify your code such that the division happens at the peaks, zeroes, and valleys of column 4. Is there a way to make that new cell output be?:
  • cell1: peak to zero
  • cell2: zero to valley
  • cell3: valley to zero
  • cell4: zero to peak
  • cell5: peak to zero, etc.
This is what I've done:
% Locate the zeros in the 4th column
index_0 = find(s_P1_30(:,4)==0);
% Find peaks
index_1 = find(s_P1_30(:,4)==max(s_P1_5(:,4)));
% Find valleys
index_2 = find(s_P1_30(:,4)==min(s_P1_5(:,4)));
% Include the start and end indices
idx = [0 index_0' index_1' index_2' size(s_P1_5,1)];
% Create individual cells containing the sub matrices
out = mat2cell(s_P1_5, diff(idx))
But I'm getting the wrong output. Instead of making the correct cells, I'm taking one huge cells at the end. Is there a way to fix this?
Thank you again!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by