How do I only keep the rows that match the values of the rows in a different list?

조회 수: 27 (최근 30일)
lil brain
lil brain 2022년 7월 17일
댓글: Voss 2022년 7월 18일
Hi,
I have this large cell array (low_condition) that contains participant IDs in the first row. I have a second list of IDs (valid_IDs) that I would like to compare witht he cell array and then only keep those participant IDs that are found in the second list. All of the remaining IDs (rows) I want to have deleted. How do I do this?
Thanks

답변 (1개)

Voss
Voss 2022년 7월 17일
load low_condition
load valid_low_IDs
valid_low_IDs contains character vectors that appear to be file names:
valid_low_IDs
valid_low_IDs = 17×1 cell array
{'5beeea64a79efb0001faf30a.csv' } {'5db9e0044a8eb40431d5e782.csv' } {'5e8ef2059eeb7a1af7502f3c - edited.csv'} {'5e67640d82b4784eb93f1a32.csv' } {'5ed5aeea88fecc14779b4a29 - edited.csv'} {'5f8998f6bfe9d6067c4b8a72.csv' } {'5fc959ba27fabf4111b918ec - edited.csv'} {'5ff0a3d22c2ee6fa5d47c312 - edited.csv'} {'59a9c0291b7a550001d6392e.csv' } {'60b43c11dcee60f547e013ba.csv' } {'60fdf891d19fd767ebc5eefc.csv' } {'598fa1b80675b100014dacf3.csv' } {'614e51ba8bd7590a59e92f9c.csv' } {'6019ec157baab31e1ad590fc.csv' } {'6079c41a26b231701ceb1e0a.csv' } {'6089aa8120d7418a70f3eba7.csv' } {'60243c357e64550d96eb47f6.csv' }
So the non-ID parts of those file names need to be removed. Here's one way:
IDs = strrep(strrep(valid_low_IDs,' - edited',''),'.csv','')
IDs = 17×1 cell array
{'5beeea64a79efb0001faf30a'} {'5db9e0044a8eb40431d5e782'} {'5e8ef2059eeb7a1af7502f3c'} {'5e67640d82b4784eb93f1a32'} {'5ed5aeea88fecc14779b4a29'} {'5f8998f6bfe9d6067c4b8a72'} {'5fc959ba27fabf4111b918ec'} {'5ff0a3d22c2ee6fa5d47c312'} {'59a9c0291b7a550001d6392e'} {'60b43c11dcee60f547e013ba'} {'60fdf891d19fd767ebc5eefc'} {'598fa1b80675b100014dacf3'} {'614e51ba8bd7590a59e92f9c'} {'6019ec157baab31e1ad590fc'} {'6079c41a26b231701ceb1e0a'} {'6089aa8120d7418a70f3eba7'} {'60243c357e64550d96eb47f6'}
On the other hand, low_condition(:,1) is a cell array of (scalar) string arrays:
disp(low_condition(:,1))
{["59a9c0291b7a550001d6392e" ]} {["60243c357e64550d96eb47f6" ]} {["6019ec157baab31e1ad590fc" ]} {["614e51ba8bd7590a59e92f9c" ]} {["60fdf891d19fd767ebc5eefc" ]} {["5beeea64a79efb0001faf30a" ]} {["5fc959ba27fabf4111b918ec" ]} {["6079c41a26b231701ceb1e0a" ]} {["5d6069f53f69fd0001c0e77f" ]} {["6033acf84fec17043ffef374" ]} {["5efd8bbe8f20d51cd2ab9327" ]} {["608d4e91d08ff4d32dbba3ad" ]} {["60b43c11dcee60f547e013ba" ]} {["615d8b5b98addf4b3d65c41d" ]} {["5ff0a3d22c2ee6fa5d47c312" ]} {["60cef6bc855784dee78ffffd" ]} {["6089aa8120d7418a70f3eba7" ]} {["5c6400cbcb937d00012d8866" ]} {["611cf405ca55c42b27807c3c" ]} {["60b65dd419636fbf792d4470" ]} {["5db9e0044a8eb40431d5e782" ]} {["5dc32687547c1f24648b7c23" ]} {["61742bd3cd78a9f853168cef" ]} {["615c343a80f9bb6fd7362271" ]} {["615db68d9d021708b6e64603" ]} {["5e67640d82b4784eb93f1a32" ]} {["5f8998f6bfe9d6067c4b8a72" ]} {["60ac8807aaad354f950dd131" ]} {["608ac09acb49bef04cade936" ]} {["https://we.tl/t-eeVxbz35Vq?src=dnl"]} {["5e83b43765a5720e30b71b37" ]} {["5ed5aeea88fecc14779b4a29" ]} {["5d175bfe7a5f6d001a64b545" ]} {["598fa1b80675b100014dacf3" ]} {["5aa67d76f053610001726e65" ]} {["61133a6bb907d515d390d08b" ]} {["5e8ef2059eeb7a1af7502f3c" ]} {["" ]}
Convert it to a cell array of character vectors, stored here as cond:
cond = cellstr(low_condition(:,1));
disp(cond)
{'59a9c0291b7a550001d6392e' } {'60243c357e64550d96eb47f6' } {'6019ec157baab31e1ad590fc' } {'614e51ba8bd7590a59e92f9c' } {'60fdf891d19fd767ebc5eefc' } {'5beeea64a79efb0001faf30a' } {'5fc959ba27fabf4111b918ec' } {'6079c41a26b231701ceb1e0a' } {'5d6069f53f69fd0001c0e77f' } {'6033acf84fec17043ffef374' } {'5efd8bbe8f20d51cd2ab9327' } {'608d4e91d08ff4d32dbba3ad' } {'60b43c11dcee60f547e013ba' } {'615d8b5b98addf4b3d65c41d' } {'5ff0a3d22c2ee6fa5d47c312' } {'60cef6bc855784dee78ffffd' } {'6089aa8120d7418a70f3eba7' } {'5c6400cbcb937d00012d8866' } {'611cf405ca55c42b27807c3c' } {'60b65dd419636fbf792d4470' } {'5db9e0044a8eb40431d5e782' } {'5dc32687547c1f24648b7c23' } {'61742bd3cd78a9f853168cef' } {'615c343a80f9bb6fd7362271' } {'615db68d9d021708b6e64603' } {'5e67640d82b4784eb93f1a32' } {'5f8998f6bfe9d6067c4b8a72' } {'60ac8807aaad354f950dd131' } {'608ac09acb49bef04cade936' } {'https://we.tl/t-eeVxbz35Vq?src=dnl'} {'5e83b43765a5720e30b71b37' } {'5ed5aeea88fecc14779b4a29' } {'5d175bfe7a5f6d001a64b545' } {'598fa1b80675b100014dacf3' } {'5aa67d76f053610001726e65' } {'61133a6bb907d515d390d08b' } {'5e8ef2059eeb7a1af7502f3c' } {0×0 char }
Now you can compare the two using ismember, and keep the relevant rows of low_condition:
to_keep = ismember(cond,IDs);
low_condition_keep = low_condition(to_keep,:)
low_condition_keep = 17×49 cell array
{["59a9c0291b7a550001d6392e"]} {[2]} {[4]} {[3]} {[3]} {[4]} {[5]} {[5]} {[4]} {[4]} {[5]} {[5]} {[4]} {[5]} {[4]} {[5]} {[4]} {[3]} {[4]} {[4]} {[5]} {[4]} {[4]} {[3]} {[4]} {[4]} {[4]} {[5]} {[4]} {[2]} {[2]} {[3]} {[2]} {[4]} {[3]} {[4]} {[4]} {[2]} {[4]} {[3]} {[2]} {[4]} {[3]} {[4]} {[5]} {[4]} {[2]} {[4]} {[4]} {["60243c357e64550d96eb47f6"]} {[2]} {[1]} {[4]} {[4]} {[1]} {[5]} {[1]} {[4]} {[4]} {[4]} {[4]} {[3]} {[4]} {[4]} {[3]} {[2]} {[2]} {[4]} {[4]} {[3]} {[4]} {[2]} {[4]} {[4]} {[5]} {[4]} {[4]} {[4]} {[1]} {[2]} {[2]} {[5]} {[5]} {[4]} {[3]} {[4]} {[2]} {[5]} {[4]} {[4]} {[2]} {[4]} {[5]} {[5]} {[4]} {[2]} {[3]} {[5]} {["6019ec157baab31e1ad590fc"]} {[2]} {[3]} {[1]} {[3]} {[3]} {[5]} {[5]} {[4]} {[4]} {[5]} {[5]} {[3]} {[3]} {[3]} {[5]} {[4]} {[1]} {[4]} {[4]} {[5]} {[1]} {[2]} {[2]} {[3]} {[5]} {[3]} {[5]} {[5]} {[1]} {[2]} {[3]} {[3]} {[5]} {[1]} {[4]} {[4]} {[2]} {[5]} {[2]} {[2]} {[5]} {[5]} {[5]} {[3]} {[4]} {[1]} {[4]} {[5]} {["614e51ba8bd7590a59e92f9c"]} {[2]} {[2]} {[1]} {[3]} {[2]} {[5]} {[2]} {[3]} {[4]} {[2]} {[2]} {[4]} {[4]} {[2]} {[3]} {[2]} {[1]} {[3]} {[5]} {[4]} {[1]} {[2]} {[1]} {[3]} {[4]} {[4]} {[4]} {[3]} {[1]} {[1]} {[2]} {[4]} {[4]} {[1]} {[3]} {[2]} {[3]} {[4]} {[3]} {[2]} {[2]} {[3]} {[4]} {[3]} {[2]} {[1]} {[3]} {[4]} {["60fdf891d19fd767ebc5eefc"]} {[2]} {[3]} {[1]} {[4]} {[3]} {[4]} {[4]} {[4]} {[5]} {[4]} {[4]} {[2]} {[3]} {[2]} {[4]} {[4]} {[2]} {[4]} {[1]} {[4]} {[1]} {[3]} {[2]} {[2]} {[4]} {[3]} {[5]} {[4]} {[1]} {[2]} {[2]} {[4]} {[5]} {[1]} {[4]} {[4]} {[1]} {[4]} {[2]} {[2]} {[3]} {[2]} {[4]} {[4]} {[4]} {[2]} {[4]} {[5]} {["5beeea64a79efb0001faf30a"]} {[2]} {[2]} {[1]} {[4]} {[2]} {[5]} {[1]} {[4]} {[4]} {[2]} {[5]} {[2]} {[4]} {[2]} {[4]} {[2]} {[2]} {[2]} {[5]} {[4]} {[1]} {[1]} {[1]} {[4]} {[4]} {[4]} {[5]} {[4]} {[1]} {[1]} {[1]} {[4]} {[4]} {[1]} {[4]} {[4]} {[2]} {[4]} {[1]} {[2]} {[4]} {[5]} {[4]} {[5]} {[2]} {[1]} {[4]} {[5]} {["5fc959ba27fabf4111b918ec"]} {[2]} {[1]} {[4]} {[1]} {[2]} {[4]} {[2]} {[4]} {[4]} {[2]} {[3]} {[4]} {[1]} {[4]} {[2]} {[4]} {[4]} {[4]} {[3]} {[4]} {[5]} {[1]} {[1]} {[1]} {[1]} {[1]} {[4]} {[3]} {[4]} {[1]} {[2]} {[4]} {[3]} {[4]} {[3]} {[4]} {[2]} {[1]} {[1]} {[2]} {[2]} {[2]} {[3]} {[4]} {[2]} {[2]} {[3]} {[3]} {["6079c41a26b231701ceb1e0a"]} {[2]} {[5]} {[5]} {[4]} {[5]} {[5]} {[5]} {[5]} {[5]} {[5]} {[5]} {[4]} {[4]} {[5]} {[5]} {[5]} {[4]} {[5]} {[5]} {[5]} {[3]} {[4]} {[4]} {[4]} {[5]} {[5]} {[5]} {[4]} {[2]} {[3]} {[4]} {[5]} {[5]} {[2]} {[5]} {[5]} {[4]} {[5]} {[4]} {[4]} {[5]} {[5]} {[5]} {[5]} {[5]} {[2]} {[5]} {[5]} {["60b43c11dcee60f547e013ba"]} {[2]} {[2]} {[2]} {[3]} {[3]} {[2]} {[4]} {[2]} {[4]} {[3]} {[2]} {[2]} {[2]} {[2]} {[4]} {[2]} {[4]} {[4]} {[4]} {[3]} {[3]} {[1]} {[2]} {[2]} {[3]} {[3]} {[3]} {[4]} {[3]} {[2]} {[1]} {[3]} {[3]} {[2]} {[3]} {[3]} {[2]} {[2]} {[2]} {[3]} {[3]} {[2]} {[4]} {[4]} {[2]} {[3]} {[3]} {[3]} {["5ff0a3d22c2ee6fa5d47c312"]} {[2]} {[1]} {[4]} {[1]} {[1]} {[3]} {[1]} {[1]} {[1]} {[1]} {[3]} {[3]} {[3]} {[3]} {[2]} {[1]} {[2]} {[3]} {[4]} {[3]} {[3]} {[3]} {[1]} {[1]} {[2]} {[3]} {[3]} {[4]} {[3]} {[1]} {[1]} {[1]} {[3]} {[3]} {[3]} {[3]} {[1]} {[2]} {[1]} {[1]} {[2]} {[2]} {[3]} {[3]} {[2]} {[1]} {[2]} {[2]} {["6089aa8120d7418a70f3eba7"]} {[2]} {[3]} {[2]} {[3]} {[3]} {[4]} {[3]} {[4]} {[4]} {[2]} {[4]} {[2]} {[4]} {[2]} {[4]} {[4]} {[4]} {[4]} {[4]} {[4]} {[2]} {[2]} {[2]} {[4]} {[4]} {[3]} {[4]} {[4]} {[2]} {[1]} {[2]} {[2]} {[4]} {[2]} {[2]} {[4]} {[1]} {[4]} {[3]} {[2]} {[3]} {[2]} {[4]} {[4]} {[2]} {[2]} {[2]} {[4]} {["5db9e0044a8eb40431d5e782"]} {[2]} {[3]} {[1]} {[5]} {[4]} {[4]} {[4]} {[5]} {[4]} {[5]} {[4]} {[2]} {[4]} {[3]} {[5]} {[4]} {[1]} {[3]} {[5]} {[5]} {[1]} {[4]} {[3]} {[4]} {[5]} {[4]} {[4]} {[3]} {[1]} {[4]} {[4]} {[2]} {[5]} {[1]} {[4]} {[4]} {[2]} {[5]} {[4]} {[4]} {[5]} {[5]} {[4]} {[4]} {[3]} {[1]} {[4]} {[4]} {["5e67640d82b4784eb93f1a32"]} {[2]} {[2]} {[3]} {[2]} {[2]} {[4]} {[2]} {[3]} {[4]} {[4]} {[4]} {[3]} {[2]} {[2]} {[3]} {[3]} {[2]} {[4]} {[4]} {[4]} {[2]} {[2]} {[2]} {[4]} {[4]} {[4]} {[4]} {[4]} {[2]} {[2]} {[2]} {[3]} {[4]} {[1]} {[3]} {[3]} {[2]} {[4]} {[4]} {[2]} {[3]} {[1]} {[4]} {[4]} {[2]} {[2]} {[3]} {[4]} {["5f8998f6bfe9d6067c4b8a72"]} {[2]} {[4]} {[4]} {[4]} {[2]} {[4]} {[4]} {[4]} {[4]} {[4]} {[4]} {[4]} {[4]} {[4]} {[4]} {[4]} {[2]} {[4]} {[4]} {[4]} {[4]} {[2]} {[2]} {[4]} {[4]} {[4]} {[4]} {[4]} {[1]} {[1]} {[1]} {[1]} {[4]} {[2]} {[2]} {[4]} {[2]} {[4]} {[4]} {[4]} {[4]} {[4]} {[4]} {[4]} {[4]} {[2]} {[2]} {[4]} {["5ed5aeea88fecc14779b4a29"]} {[2]} {[4]} {[2]} {[5]} {[4]} {[4]} {[4]} {[4]} {[4]} {[4]} {[3]} {[4]} {[5]} {[3]} {[5]} {[4]} {[3]} {[3]} {[5]} {[4]} {[2]} {[4]} {[4]} {[4]} {[5]} {[5]} {[3]} {[4]} {[2]} {[3]} {[3]} {[4]} {[4]} {[1]} {[4]} {[5]} {[3]} {[4]} {[5]} {[3]} {[5]} {[4]} {[4]} {[5]} {[5]} {[1]} {[4]} {[5]} {["598fa1b80675b100014dacf3"]} {[2]} {[3]} {[5]} {[2]} {[4]} {[5]} {[3]} {[4]} {[5]} {[3]} {[4]} {[2]} {[4]} {[5]} {[3]} {[4]} {[5]} {[4]} {[5]} {[4]} {[5]} {[1]} {[4]} {[3]} {[5]} {[4]} {[5]} {[2]} {[4]} {[2]} {[2]} {[4]} {[5]} {[4]} {[3]} {[5]} {[2]} {[5]} {[4]} {[4]} {[4]} {[4]} {[5]} {[4]} {[3]} {[4]} {[4]} {[5]}
  댓글 수: 2
Voss
Voss 2022년 7월 18일
You're welcome! Any questions, let me know. Otherwise, please "Accept This Answer". Thanks!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by