Splitting up a matrix into X sizes/ based on specified values in a matrix

2 ビュー (過去 30 日間)
mark
mark 2013 年 12 月 28 日
コメント済み: mark 2013 年 12 月 28 日
If I have a matrix of size 2x100 of time vs position values (or whatever). I we say time goes from 0s to 2s then I would like to write a script that says:
" Find where X(1,:) = 1 and split it, and X(2,:), such that I have two new matrices A and B which correspond to the values from 0s to 1s and 1.1s to 2s"
I tried using the find feature but that didn't work. Any ideas?
Essentially, if I have 10s of run time then I'd like to be able to separate those 10s into 10 separate matrices.
Cheers, Mark.
EDIT:
I should add that the time scale may not be equal as the time sampling is automated based on when an event occurs. This is to reduce the computational time substantially and maintain a high enough resolution at an event. So, the important aspect is to be able to specify the points on the matrix that I'd like to separate out!
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 12 月 28 日
It's better to post your data or code as a text than an image

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

採用された回答

Wayne King
Wayne King 2013 年 12 月 28 日
編集済み: Wayne King 2013 年 12 月 28 日
Are the time instants just integers? If not, that may be why find() does not work depending on how you are using it.
X = ones(2,100);
X(1,:) = 0:99;
idx1 = find(X(1,:)<=50,1,'last');
idx2 = find(X(1,:)>50,1,'first');
A = X(:,1:idx1);
B = X(:,idx2:end);

その他の回答 (1 件)

Baalzamon
Baalzamon 2013 年 12 月 28 日
Logical indexing is one way. Both Doug and Loren have a mini tutorial on it. To get say all elements of X(1,:), that are say 0-50, replace the ':' with a logical argument. e.g. A = X(1, X(1,:) > 50 ); B = X(1, X(1,:) < 51); for both rows A = X(:, X(1,:) > 50); B = X(:, X(1,:) < 51);
The 'X(1,:) > 50' bit is looking up all elements in the first row of X that is GT 50, and then using that to create the new array.
As you can guess it is possible to use multiple logical arguments with '&&' etc.
  1 件のコメント
mark
mark 2013 年 12 月 28 日
Thanks guys, the logical stuff works.
if true
X1=0:0.5:100;
X2=0:0.25:50;
X=[X1' X2'];
Ax = find(X(:,1)<=50);
A1=[X1(Ax)];
A2=[X2(Ax)];
A=[A1' A2'];
Bx = find(X(:,1)>50);
B1=[X1(Bx)];
B2=[X2(Bx)];
B=[B1' B2'];
end

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

カテゴリ

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