How to access the index of the cell elements

조회 수: 120 (최근 30일)
surendra kumar Aralapura mariyappa
답변: Vimal Rathod 2019년 7월 16일
Hey all,
Hope everyone doing good. I have a small doubt in accessing the index of a cell element in a cell array.
I have a cell array,
A = {1 2 'Node1'; 3 4 'Node2'};
p = cell2mat(X(:,1));
%q = cell2mat(X(:,2));
disp(p);
%disp(q);
T_Vorgabe = zeros(1,length(f));
for plotnumber = 1:n
c = find(p == plotnumber,1);
if ~isempty(c)
c = plotnumber;
% Here I need to get the index of the cell element, which means plotnumber or p.
end
end
My question is how to find the index of the cell element of a first column.
for an example if plotnumber is 3, index of 3 is 2,1.
Any suggestions and answered are most welcomed
Thanks in advance
  댓글 수: 2
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 6월 29일
Suppose a cell array
sanmple_stuc={[1 2 3 4],[4 5 6],[ 6 7 8]}
Now you are looking for the index of respective array element? Yes, plear clarify?
Say 6 is which which cell array and respective index positions??
surendra kumar Aralapura mariyappa
@ Kalyan acharjya,
This is my cell array,
A = {1 2 'Node1'; 3 4 'Node2'};
Now I just need to access the first column of the cell array like this.using this A{:,1}.
p = cell2mat(A(:,1));
T_Vorgabe = zeros(1,length(f));
for plotnumber = 1:n
c = find(p == plotnumber,1);
if ~isempty(c)
c = plotnumber;
% some other calculations
% Here I need to get the index of the cell element, which means plotnumber or p.
end
end
When the plotnumber is 1, c = plotnumber, when the plotnumber is 3, c = plotnumber,
So Now I need to know the index of this two number inside the cell array for the later use.
Just like A{1,1} = 1; A{2,1} = 3. just the index of the first column cell elements.
Thanks in advance

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

답변 (1개)

Vimal Rathod
Vimal Rathod 2019년 7월 16일
Hi I understand that you want to find the index of any element in a cell and from the example you gave, I assume that the element whose index to find is a number. You can use the “find function” to find the element index in the cell array.
% Making a cell as an array using [A{:}].
% Using find function on that can help you out.
A = {1,2,'Node1';3,4,'Node2'};
% this will return the index 2 which is a matlab version of indexing
%Using column first and row as the second input as ':' operator prints
%the cell in column major order.
[column,row] = find([A{:}] == 3);
You can refer the find function documentation here
You can also refer to a similar problem here
If you want to find the index of string in the cell you can use “for loop” to loop through the elements.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by