Issue with 'intersect' arrays function

조회 수: 1 (최근 30일)
Nia
Nia 2015년 8월 21일
편집: Nia 2015년 8월 26일
I am trying to intersect two arrays but I keep getting the following error: "Input A of class cell and input B of class cell must be cell arrays of strings, unless one is a string".
My arrays look like this:
and
I cannot find the way to intersect them. I have also tried:
[dur, itimes, inewtimes ] = intersect(array2,char(array1));
but no luck.
I attach here the arrays that I am trying to intersect.

답변 (2개)

Adam
Adam 2015년 8월 21일
편집: Adam 2015년 8월 21일
You need to convert your times array to strings if you want to compare them. You can't compare a time with a string, whether with intersect or just with a general equality test.
array1 = arrayfun( @(x) char(x), array1, 'UniformOutput', false )
intersect( array2, array1 )
  댓글 수: 13
Nia
Nia 2015년 8월 21일
for some reason the elements on my array2 appear within quotation marks and that may be the cause of not being able to intersect these two arrays
Nia
Nia 2015년 8월 21일
using:
array2=cellfun( @(x) num2str(x), array2, 'UniformOutput', false )
I get the following error: Undefined function 'abs' for input arguments of type 'cell'

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


Steven Lord
Steven Lord 2015년 8월 21일
The reason that you're having so much trouble is that your second input is a cell array of strings. When you call INTERSECT or any of the set functions with one of the inputs being a cell array of strings, both inputs must be cell arrays of strings (or a plain char vector.) Convert that second input into a duration array then you can do what I do in this example. Create two sample duration vectors.
A = seconds(0:5:seconds(hours(1)));
B = minutes(10)+seconds([1 3 5 7 9]);
Intersect them.
[commonDurations, ind1, ind2] = intersect(A, B);
Check the results. To show the results using consistent units, convert B's display format to seconds first. This doesn't change the data stored in B, just the way that data is displayed.
commonDurations
B.Format = 's';
B(ind2)
A(ind1)
  댓글 수: 1
Nia
Nia 2015년 8월 21일
@Steven: How can I convert my cell array of strings to a duration array?. I cannot find the function that does that.

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by