How do I use the ismember function (if possible) to locate the min/max GMT time elements within a cell array?

조회 수: 10(최근 30일)
Brad
Brad 2016년 2월 4일
댓글: Brad 2016년 2월 6일
I have the following 5x4 cell array;
z = { '21:48:50.53' 78530.5390000000 2 580;
'21:48:51.28' 78531.2890000000 4 581;
'21:48:52.03' 78532.0390000000 0 4;
'21:48:52.80' 78532.8050000000 0 4;
'21:48:53.55' 78533.5550000000 1 4};
I am attempting to create a subset of data (A) based on the values in column 4 using the following;
A = z(ismember(z(:, 4), [4]), :);
with the intent of attaining the minimum and maximum values in column 1 (GMT Time).
So using the above cell array, the minimum (earliest) time would be 21:48:52.03 and the maximum (latest) time would be 21:48:53.55.
However, when I attempt to create the subset of data, I get the following error message;
??? Error using ==> cell.ismember at 28 Input must be cell arrays of strings.
Error in ==> GMT_Time_Extraction at 10 A = a(ismember(a(:, 4), [4]), :);
I suspect the problem rests in the GMT times in column 1 or the way I’m attempting to use the ismember function. But in reading the documentation I’m not sure what I’m missing.
Any suggestions are greatly appreciated.
Thank you.

채택된 답변

David Young
David Young 2016년 2월 4일
편집: David Young 2016년 2월 4일
The problem is that you are passing ismember a cell array containing numbers, but it can handle cell arrays only if all they contain is strings. You can easily convert the cell array of numbers to an ordinary numerical array, for example with
A = z(ismember([z{:, 4}], [4]), :);
which uses {} to extract all the elements of column 4 from their cells, then an outer [] to concatenate those into a numerical vector.
Alternatively, you can do the conversion with an explicit call:
A = z(ismember(cell2mat(z(:, 4)), [4]), :);
Note the difference between z{...} in the first method and z(...) in this one.
Since you only want to see whether the value is equal to 4, ismember is overkill. You can more simply and readably use
A = z([z{:, 4}] == 4, :);
or if you prefer
A = z(cell2mat(z(:, 4)) == 4, :);
Finding the maximum and minimum times in column 1 is a separate problem, but easily tackled using datenum.
  댓글 수: 1
Brad
Brad 2016년 2월 6일
David, thanks for taking a look at this. Problem solved and the min/max values in column 1 have been ID'ed.

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

추가 답변(0개)

제품

Community Treasure Hunt

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

Start Hunting!

Translated by