How to access the index of the cell elements

171 views (last 30 days)
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 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 29 Jun 2019
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

Sign in to comment.

Answers (1)

Vimal Rathod
Vimal Rathod on 16 Jul 2019
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.

Categories

Find more on Data Types in Help Center and File Exchange

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!