필터 지우기
필터 지우기

How to return the index of a cell array?

조회 수: 2 (최근 30일)
Sara Al Shamsi
Sara Al Shamsi 2016년 5월 23일
편집: Nut 2016년 5월 23일
Hi,
My code consists of 3 parts:
1- to select randomly a "cell" from a cell array. 2- return the index of the selected "cell". 3- delete the selected cell from the original cell array.
I did the first part and I got:
X =
[ 98.5004]
[4x1 double]
[5x1 double]
Then, I wanted to select a cell randomly from X and I got (example):
[4x1 double]
which is the second cell.
So, How to return its index? (manually its index is X(2)). what if I have a lot of cells? I know that "find" can be used for normal matrices but when I tried to do the same for the cell arrays, it didn't work! I read about the "cellfun" but it confused me!
last part depends on the second part! I will not be able to delete the selected cell from (X) without knowing its index! I want to delete it because when I will select randomly again from X, " I don't want to select again the same cell!

채택된 답변

Nut
Nut 2016년 5월 23일
편집: Nut 2016년 5월 23일
Hi,
is this example good for you?
X{1} = 1;
X{2} = [2 2 2 2];
X{3} = [3 4 5 6 7];
random_index = randi(length(X));
random_cell = X(random_index);
X{random_index} = [];
X = X(~cellfun('isempty',X));

추가 답변 (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