take second element of cell
11 次查看(过去 30 天)
显示 更早的评论
Dear all, I have 500 cell this is part of it, a={[1,5] [2,36,74,56,73,5] [3,79,73,5] [4,74,56,73,5] [5] [6,12,5] [7,3,79,73,5] [8,25,97,69,61,3,79,73,5]}; I just want to take the second element from each cell and put it in spreat it array like:
b=[5 36 79 74 0 12 3 25];
Thanks...
0 个评论
采纳的回答
the cyclist
2016-12-25
编辑:the cyclist
2016-12-25
Here's one way. I needed to make the temporary variable to accommodate the way you are handling the cell array element whose vector is length 1.
tmp = cellfun(@(x)[x 0],a,'UniformOutput',false);
b = cellfun(@(x)x(2),tmp)
clear tmp
2 个评论
Image Analyst
2016-12-25
Amazing yes. For anyone finding it a bit cryptic, here's a explicit, brute force method using a for loop -- maybe not as efficient for long arrays, or MATLAB-ish, but possibly more intuitive and understandable for some readers:
a={[1,5] [2,36,74,56,73,5] [3,79,73,5] [4,74,56,73,5] [5] [6,12,5] [7,3,79,73,5] [8,25,97,69,61,3,79,73,5]}
for k = 1 : length(a)
thisCellContents = a{k};
if length(thisCellContents) > 1
% Normal case
b(k) = thisCellContents(2);
else
% Only one element in the vector so assign it 0
b(k) = 0;
end
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!