Hellow everyone! I have 2 cells.
A = '1,2,3';
B= '1,4,5,6';
But, When i try to use intersect function to get the common element(s) as my result, say
C = intersect(A, B);
I am getting an empty cell like this.
C =
Empty cell array: 1-by-0
Kindly help me, My result shall show like
C='1'
Your help will be highly appreciated!

댓글 수: 4

Hi,
I think your A and B are:
A = {'1,2,3'};
B = {'1,4,5,6'};
This means A is a cell containing the string '1,2,3' and B is a cell containing the string '1,4,5,6'. These strings are different, so the intersection of A and B is empty.
To get what you want, the numbers should be separated (different strings, or just numbers).
How are A and B defined in your code?
Stephen23
Stephen23 2017년 6월 22일
"Problem with "Intersect" function"
There is no problem with intersect: you have given it two cell arrays containing different strings, so clearly the intersection is an empty cell array. This is exactly as the documentation states.
pradeep kumar
pradeep kumar 2017년 6월 22일
@ Alice! Yes, A & B are different cells and contain strings. Please tell me how can i break them to individual number (or strings) to get my job done.
@ Stephen Cobeldick , You mean to say my cells should look like the following .
A={'1','2','3'};
B={'1','4','5','6'};
Kindly tell me, how can i achieve this?

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

 채택된 답변

Jan
Jan 2017년 6월 22일
편집: Jan 2017년 6월 22일

0 개 추천

But the shown code works:
A = '1,2,3';
B = '1,4,5,6';
C = intersect(A, B);
% C = ',1' % The commonly appearing characters ',' and '1'
According to the posted code, A and B are not cells, but char vectors, also called strings.
If you want to treat the numbers as numbers:
An = sscanf(A, '%g,');
Bn = sscanf(B, '%g,');
Cn = intersect(An, Bn)
% C = 1
If A and B are cells, use "A{1}" and "B{1}" instead.
It depends on your specific problem if the input data should be
A = {'1','2','3'};
or
A = [1,2,3]
Most likely you do not have to convert the cell string {'1,2,3'}, if you choose the rigth method to obtain the data at the beginning. Where do these data come from?

추가 답변 (1개)

KSSV
KSSV 2017년 6월 22일

0 개 추천

A = [1,2,3] ;
B = [1,4,5,6];
intersect(A,B)
OR
A = '1,2,3';
B= '1,4,5,6';
intersect(str2num(A),str2num(B))

댓글 수: 1

@ KSSV, str2num doesn't work, since A and B are 2 cells. It shows the following error.
Error using str2num (line 32)
Requires string or character array input.
Kindly help me.

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

카테고리

도움말 센터File Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품

질문:

2017년 6월 22일

편집:

Jan
2017년 6월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by