필터 지우기
필터 지우기

How to extract a column from cell arrays matrix?

조회 수: 164 (최근 30일)
Abdulaziz Abutunis
Abdulaziz Abutunis 2017년 4월 29일
댓글: Abdulaziz Abutunis 2018년 8월 16일
Dear MATLAB users,
I wish I got your help for this problem. I have built a cell arrays as a matrix as follow
for k=1:5
out{k}(1,:}=
out{k}(2,:}=
out{k}(3,:}=
end
I am trying to extract the the first element in first array from each cell. i have tried this out{1:5}(1,1) but it does not work however if i type out{1}(1,1) this give the first element but in specific cell also out{1:5} shows all cell matrices. The error I saw when i tried out{1:5}(1,1) was Expected one output from a curly brace or dot indexing expression, but there were 5 results.
Thank you in advance. Best Aziz
  댓글 수: 3
Abdulaziz Abutunis
Abdulaziz Abutunis 2017년 4월 29일
Thank you Stephen, you are totally right. I just do some adjustment to an old code. I will consider your advice in the future. Appreciate it
Walter Roberson
Walter Roberson 2017년 4월 29일
Abdulaziz Abutunis comments to Stephen:
He did not solve the problem but he did give a good advice to avoid this problem in the future. he even gave more than one option.

댓글을 달려면 로그인하십시오.

채택된 답변

per isakson
per isakson 2017년 4월 29일
One way
>> cellfun( @(a) a(1,1), out )
ans =
0.9572 0.0357 0.7060 0.0344 0.7094
however, a for-loop might be a better choice
  댓글 수: 6
per isakson
per isakson 2017년 4월 29일
@Abdulaziz Abutunis, Create some sample data
for ii = 1:5
for jj = 1:3
out{ii}(jj,:) = ii*100 + jj*10 + (1:3)';
end
end
Inspect the value of the first cell
>> out{1}
ans =
111 112 113
121 122 123
131 132 133
Inspect one specific element of the value of the first cell
>> out{1}(3,2)
ans =
132
>>
Caveat: I'm not sure I understood your question.
Abdulaziz Abutunis
Abdulaziz Abutunis 2018년 8월 16일
I should have said thanks again a long time ago:). I already used your suggestions
Thanks a lot

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2017년 4월 29일
편집: Image Analyst 2017년 4월 29일
You can't have out{k}(1,:} because you got two closing braces and no closing parenthesis. Maybe you meant out{k}(1,:). See the FAQ: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F and try to avoid cell arrays in most cases, definitelyif you have rectangular numerical array of constant dimensions, it just complicates things.
  댓글 수: 1
Abdulaziz Abutunis
Abdulaziz Abutunis 2017년 4월 29일
you right it was a typo sorry and thanks for pointing this out

댓글을 달려면 로그인하십시오.


Juvinch Vicente
Juvinch Vicente 2018년 8월 15일
편집: Juvinch Vicente 2018년 8월 15일
I'm not sure if this would work for the older version, but for version above 2016b, this line works.
cellMatrix = {'A', 'B', 'C'; 'A', 'B', 'C';'A', 'B', 'C'};
if you want the first column,
firstCol = cellMatrix(:,1);
and so forth.
  댓글 수: 1
Abdulaziz Abutunis
Abdulaziz Abutunis 2018년 8월 16일
Thank you Juvinch for the suggestion. I will consider when I use the cell array again

댓글을 달려면 로그인하십시오.

카테고리

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