How to crop a matrix/array based on a condition

I have a matrix consisting of 10 columns/arrays. First 9 are my data. The 10th column/array is a "trigger channel" which has sharp spikes from baseline 0V to 5V and back down to 0V. I need to crop all my columns/arrays in the matrix into sections, from spike to spike in the 10th channel. First "section" is from spike 1 to spike 2, second "section" is from spike 2 to spike 3, and so on. How would I go about doing this?
Edit: My matrix:
38 64 5
35 55 0
24 42 0
14 36 0
25 63 0
93 23 5
23 52 0
24 95 0
24 64 0
46 23 5
24 68 0
48 22 0
18 42 5
35 55 0
24 42 0
14 36 5
25 63 0
93 23 0
23 52 0
24 95 0
I need to horizontally cut the matrix shown above into smaller matrices. Each "submatrix" should start and end with when the third array changes from 0 to 5. The matrix above should be cropped into 5 matrices.

5 commentaires

Can you post a short example? a 6x3 matrix for example, and post the expected result
Karan Gill
Karan Gill le 24 Juin 2016
To crop a matrix/array based on a condition, logical indexing is what you use: http://blogs.mathworks.com/loren/2013/02/20/logical-indexing-multiple-conditions/
Of course, your specific situation might not be so straightforward.
Attach your data with the paper clip icon.
%%%modify these
%%%in must be column major
%%%trigger must be column major and have same # of column as in
in = [
38 64 5;
35 55 0;
24 42 0;
14 36 0;
25 63 0;
93 23 5;
23 52 0;
24 95 0;
24 64 0;
46 23 5;
24 68 0;
48 22 0;
18 42 5;
35 55 0;
24 42 0;
14 36 5;
25 63 0;
93 23 0;
23 52 0;
24 95 0;];
in = in';
trigger = in(3,:);
in = in(1:2,:);
threshold = 2;
%%%end modify
num_subarray = sum(trigger>threshold);
out = cell(num_subarray,1);
prev_i = 0;
out_i = 1;
for i = 1:numel(trigger)
if(trigger(i) > threshold)
num_ele = i - prev_i;
out{out_i} = zeros(size(in,1),num_ele);
out{out_i}(:,1:num_ele) = in(:,prev_i+1:i);
out_i = out_i + 1;
prev_i = i;
end
end
%%%output stored in out
%%%each submatrix is accessed by out{i}
%%%each submatrix element is accessed by out{i}(j,k)
This does NOT have 10 columns. It has 3. So is the last column the "spike" column? If so, the spikes are easily found and the problem is solved very easily with the regionprops() function.

Connectez-vous pour commenter.

Réponses (1)

Andrei Bobrov
Andrei Bobrov le 28 Juin 2016
Modifié(e) : Andrei Bobrov le 28 Juin 2016
A = [38 64 5
35 55 0
24 42 0
14 36 0
25 63 0
93 23 5
23 52 0
24 95 0
24 64 0
46 23 5
24 68 0
48 22 0
18 42 5
35 55 0
24 42 0
14 36 5
25 63 0
93 23 0
23 52 0
24 95 0]
m = size(A,1);
out = accumarray(cumsum(A(:,3) > 0),(1:m)',[],@(x){A(x,:)});
or
trsh = 2;
t = [true;diff( A(:,3) > trsh)==-1];
z = find(t,1,'last')-1;
a = A(1:z,1:end-1);
out = accumarray(cumsum(t(1:z)),(1:z)',[],@(x){a(x,:)});

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by