How do I make cell filttering

Dear all, I facing problem with cell array. For example; I have cell
a= { [1 2 3 4] [7 4] [ 3 5] [ 3 6 7 4]}
How I know each array repeat it number 4 in it and take first element in array?
Results1= {[1 2 3 4] [7 4] [ 3 6 7 4]};
Results2= [ 1 7 3];
Thanks…

3 Commenti

Jan
Jan il 27 Gen 2017
Modificato: Jan il 27 Gen 2017
Please do not bump a thread without providing new information. Getting no answer is a secure indicator of a question, which is not clear.
What does "repeat it number 4 in it and take first element in array" mean? I do not see the relation between this and the shown wanted result. Do you want to find all arrays, which contain the element 4?
Guillaume
Guillaume il 27 Gen 2017
Please don't add comments as Answers.
I don't fully understand your question. Why is [3 5] removed for Results1 and why is Results2 not [1 7 3 3]?
skysky2000
skysky2000 il 27 Gen 2017
Thanks for replying.... [3 5] removed coz does not include number 4. I want only the arrays including number 4 as shown in Results1. and then take the first element from each array including number 4 as shown in Results2. Thanks...

Accedi per commentare.

 Risposta accettata

Guillaume
Guillaume il 27 Gen 2017
At a guess, you want all the arrays that contain the number 4:
a = {[1 2 3 4] [7 4] [ 3 5] [ 3 6 7 4]}
Results1 = a(cellfun(@(v) ismember(4, v), a))
And the first value of each element of Results1:
Results2 = cellfun(@(v) v(1), Results1)
If cellfun is too complex for you, the same with a loop:
a = {[1 2 3 4] [7 4] [ 3 5] [ 3 6 7 4]}
contains4 = false(size(a));
firstelement = zeros(size(a));
for idx = 1:numel(a)
contains4(idx) = ismember(4, a{idx});
firstelement(idx) = a{idx}(1);
end
Results1 = a(contains4)
Results2 = firstelement(contains4)

1 Commento

Jan
Jan il 27 Gen 2017
Funny. I've posted nearly the same code in the opposite order. Well, I will vote for your code, because I'm convinced it is efficient :-)

Accedi per commentare.

Più risposte (2)

Jan
Jan il 27 Gen 2017
Modificato: Jan il 27 Gen 2017
A bold guess: Get all arrays, which contain the value 4 and copy their first element:
a = {[1 2 3 4], [7 4], [3 5], [3 6 7 4]};
n = numel(a);
found = false(1, n);
Result2 = zeros(1, n); % Pre-allocate
iResult = 0;
for k = 1:n
if any(a{k} == 4)
found(k) = true;
Result2(k) = a{k}(1);
end
end
Result1 = a(found);
Result2 = Result2(1:nnz(found));
If this does what you need, here a compact version:
Result1 = a(cellfun(@(x) any(x==4), a));
Result2 = cellfun(@(x) x(1), Result1);
skysky2000
skysky2000 il 27 Gen 2017

0 voti

Many thanks Guillaume and Jan . its work. Please, Another question; I have one vector (c) and one cell array (H), how can i separate cells from H depend on c vector. this is just simple vector( my real vector about 200 elements. c=[4 8 7] H= {[ 2 3 4] [5 6 3 4][[5 7] [5 6 8] [ 2 1 9 7] [9 7]}
results should be : Re1= cells including first elements in c (4). Re2= cells including second element in c (8). Re3= cells including third element in c (7).
Re1={[ 2 3 4] [5 6 3 4]}; Re2={[5 6 8]}; Re3={[ 2 1 9 7] [9 7]} . . . .
Many thanks....

1 Commento

Jan
Jan il 27 Gen 2017
Modificato: Jan il 27 Gen 2017
You can create a loop around the given code.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Richiesto:

il 27 Gen 2017

Modificato:

Jan
il 27 Gen 2017

Community Treasure Hunt

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

Start Hunting!

Translated by