How to extract entire rows based on values placed in a single column in a matrix?

28 次查看(过去 30 天)
Hi everyone,
I have a 100x10 table, let's call it CH_channel. i have got 3 questions. If you are able to find a solution for every task, you will be a hero to me, otherwise, if you manage to solve only the first task, it would be really appreciated as well.
  1. in this matrix, i want to find all the values exceeding a value x (let's say 7) only in column number 5. Then I would like to retain all the values in the same row;
  2. once done this, I would like to "crop" 1 row before the x value and 2 rows after it within the CH_channel.
  3. I would like to create a new table in 3 dimension, where every "cropped" epoch represent a dimension (they are supposed to be trials).
note that if you end up with 2 consecutive rows where you have 2 or more values >7, you will need to start computing the rows to retain only from the first values exceding the x.
right now, I am stucked, please help.
the final results, according to the following table should be:
  1. for the first point, the code should retain all the values in rows 8 and 12 (for all columns);
  2. crop/cut all the values within rows 7:10 and from rows 11:14
  3. with the values "extracted" outr of the matrix CH_channel, i would like to create a brand new 3D matrix, where these "cropped" rows represent different epochs (or trials)
CH_channel = array2table(randi(8,16, 5))
CH_channel = 16×5 table
Var1 Var2 Var3 Var4 Var5 ____ ____ ____ ____ ____ 3 8 2 5 2 1 3 3 4 7 6 1 4 1 3 4 4 8 3 6 5 4 2 5 5 5 3 2 5 4 4 1 1 6 6 8 6 1 2 8 7 7 1 2 3 5 2 1 2 2 4 2 6 5 6 7 6 5 4 8 7 7 2 6 7 5 7 1 3 2 3 4 8 4 2 6 2 3 6 3
  5 个评论
Voss
Voss 2022-10-26
  1. in this matrix, i want to find all the values exceeding a value x (let's say 4) only in column number 33. Then I would like to retain all the values in the same row;
% make the table:
CH_channel = array2table(randi(10,100, 33))
CH_channel = 100×33 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 Var21 Var22 Var23 Var24 Var25 Var26 Var27 Var28 Var29 Var30 Var31 Var32 Var33 ____ ____ ____ ____ ____ ____ ____ ____ ____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____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7 3 4 6 1 2 2 8 1 8 2 9 10 1 4 1 10 3 2 10 8 6 3 10 5 5 8
% rows where column 33 > 4:
idx = CH_channel{:,33} > 4
idx = 100×1 logical array
0 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 0 1 0 1 1 0 1 0 1 1 0 0 0 0 1 0 1 0 0 1 1 0 0 1 0 1 0 1 0 0 1 1 1 1 1 1 1 1 1 0 0 1 0 1 1 1 1 1 1 0 0 1 1 0 1 1 0 1 1 0 1 1 1 1 0 1 1 1 0 0 0 0 0 1 1 1 0 0 1 0 1 1 0 1
% keep those rows:
values_to_keep = CH_channel(idx,:)
values_to_keep = 62×33 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var15 Var16 Var17 Var18 Var19 Var20 Var21 Var22 Var23 Var24 Var25 Var26 Var27 Var28 Var29 Var30 Var31 Var32 Var33 ____ ____ ____ ____ ____ ____ ____ ____ ____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ 5 4 5 1 6 8 6 2 3 1 3 1 8 7 2 5 2 5 8 2 10 3 2 9 4 1 1 6 10 6 4 6 8 9 4 3 8 6 6 3 2 1 6 10 4 5 1 3 5 1 10 10 7 4 4 8 1 4 8 7 9 9 6 4 3 6 8 3 2 9 5 3 8 3 9 10 7 6 7 6 8 4 5 6 3 8 8 10 5 4 9 10 6 8 8 8 1 7 8 10 8 6 8 5 5 3 3 6 6 10 8 10 8 10 4 3 9 6 9 5 10 1 2 3 4 8 1 1 4 8 9 5 10 9 2 8 7 10 6 3 10 4 10 5 4 4 1 5 10 10 8 8 6 8 6 8 1 10 3 6 2 9 3 9 10 4 8 1 2 4 5 8 2 3 8 2 5 5 10 1 3 9 9 10 9 2 2 5 6 10 6 4 5 2 3 4 6 8 5 8 7 3 2 1 3 5 1 7 8 5 2 4 8 6 2 6 4 10 5 3 7 6 5 9 1 2 6 8 1 1 5 4 1 8 5 7 7 1 4 10 4 1 7 10 9 6 2 4 1 3 8 1 6 9 5 6 8 4 4 9 8 6 8 6 5 3 5 8 2 6 9 9 9 2 2 6 4 6 1 3 6 10 5 1 4 1 7 3 4 8 9 8 2 8 4 1 8 10 7 8 5 9 4 4 5 1 6 7 7 1 10 9 8 6 1 7 10 9 8 3 9 9 4 3 4 9 8 10 4 9 10 2 10 9 3 4 5 5 8 6 4 1 6 4 3 9 4 8 9 6 4 5 9 7 7 9 10 2 6 1 4 7 9 6 1 6 2 4 6 7 8 2 8 6 9 9 3 4 2 5 6 10 2 6 6 3 7 9 2 3 3 9 3 9 7 7 2 7 8 2 5 10 7 3 4 6 1 2 2 8 1 8 2 9 10 1 4 1 10 3 2 10 8 6 3 10 5 5 8 2 5 3 6 7 3 9 8 2 4 2 2 10 4 8 9 5 3 10 8 8 10 2 5 4 3 10 3 9 2 4 1 6 4 10 3 3 5 7 9 6 6 10 2 9 5 2 7 7 9 3 1 8 7 6 7 10 10 3 2 2 1 2 4 10 7 10 5 9 3 2 6 5 10 9 3 5 5 7 9 2 8 8 5 3 9 7 3 2 8 7 1 6 5 2 4 10 7 10
enzo
enzo 2022-10-26
this seems like a beatiful and smooth solution for my first step tpward my final goal. thansk a lot. I will work it out and try to reach the next steps

请先登录,再进行评论。

回答(1 个)

David Hill
David Hill 2022-10-26
H = array2table(randi(10,100, 33));
f=find(H{:,33}>4);
f=f(diff(f)>1);%removes consecutive rows >4
for k=1:length(f)
c{k}=H(max(1,f(k)-5):min(100,f(k)+10),:);
end
c
c = 1×28 cell array
{11×33 table} {13×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {16×33 table} {15×33 table}

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by