Finding columns in a cell array containing finite elements

조회 수: 3 (최근 30일)
Himanshi Rani
Himanshi Rani 2017년 9월 14일
댓글: Guillaume 2017년 9월 14일
Hi, I have a m*n cell array containing x and y coordinates , suppose K is a 3*2 cell array as follows:
K=
[Inf] [1x2 double] [Inf]
[Inf] [ Inf] [1x2 double]
[Inf] [Inf] [Inf]
I want to access the columns of the array that are not infinite. My aproach: find([K{f,:}]< inf) returns columns as 2,3 for the first row. I want the result to be just 2. The x and y coordinates occur at random positions.
Is there any way to find the columns of the cell array?
Thanks

채택된 답변

Guillaume
Guillaume 2017년 9월 14일
cellfun(@(x) all(isfinite(x)), K)
will return a logical array telling you whether each cell of K contains only finite values. You can pass that to find if you do need to know the rows or columns. Most time, using the logical array as a filter is faster.
  댓글 수: 3
Stephen23
Stephen23 2017년 9월 14일
@Himanshi Rani: using logical indexing is likely the simplest and most efficient way of doing this.
Guillaume
Guillaume 2017년 9월 14일
@Himanshi Rani, every time you use find, you are using a logical array. In your original approach find([K{f,:}]< inf), the [K{f,:}]< inf is a logical array.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Specialized Power Systems에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by