take second element of cell

조회 수: 8 (최근 30일)
skysky2000
skysky2000 2016년 12월 25일
댓글: Image Analyst 2016년 12월 25일
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