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 个评论
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
2017-1-27
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
2017-1-27
采纳的回答
更多回答(2 个)
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);
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!