How to extract a value/s from a matrix that match with specific rows

5 views (last 30 days)
Hi Friends,
I have a structure matrix (V= 1x6616 struc) as shown in the attched image. I want to sellect the all rows that match with the type '1' in the second colums. I have tried this code but it does not have any result.
for i = 1:numel(V)
C{1, i} = V{1,i}(strcmpi(V{1,i}.type,'1'),:);
end
%%I have tried this one as well, but no result.
C = V({'1'},{'latency','urevent','epoch'});

Accepted Answer

Star Strider
Star Strider on 28 Jul 2022
One possibility is to use the findgroups function to select the appropriate rows, then use that to index into the data.
type = string(randi(5, 10, 1));
others = rand(10,3);
Everything = [type, others]
Everything = 10×4 string array
"5" "0.15966" "0.15112" "0.40634" "5" "0.93737" "0.24496" "0.52959" "1" "0.28389" "0.90393" "0.84487" "3" "0.66454" "0.51595" "0.26782" "1" "0.89376" "0.23557" "0.56919" "5" "0.9502" "0.53974" "0.97958" "4" "0.98398" "0.39381" "0.054346" "1" "0.65179" "0.12865" "0.53393" "5" "0.51813" "0.6268" "0.61095" "2" "0.055209" "0.62481" "0.98641"
[G,ID] = findgroups(Everything(:,1))
G = 10×1
5 5 1 3 1 5 4 1 5 2
ID = 5×1 string array
"1" "2" "3" "4" "5"
Result = Everything(G==1,:)
Result = 3×4 string array
"1" "0.28389" "0.90393" "0.84487" "1" "0.89376" "0.23557" "0.56919" "1" "0.65179" "0.12865" "0.53393"
In the absence of your data, I used the string function to create the first column. This turns ‘Everything’ into strings, however I am certain that this illustrates the concept.
.

More Answers (0)

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!