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...

采纳的回答

the cyclist
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 个评论
skysky2000
skysky2000 2016-12-25
编辑:Image Analyst 2016-12-25
That's amazing.... Thanks a lot mate!
Image Analyst
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 CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by