hello,
i have cell matrix size(1*1000) each element is matrix (10*10)
i want to test if matrix is an element of cell or not how to do it?

댓글 수: 1

amina KHARBACHE
amina KHARBACHE 2017년 7월 6일
i was lloking for simple insttruction with matlab who find directly the matrix inside the cell matrix
but with a simple loop for can do it
for i=1numel(cellmatrix) if cellmatrix{1,i}==test_matrix save_index=i; break end end
thanx in advance ^^

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

 채택된 답변

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

1 개 추천

index = [];
for k = 1:numel(C)
if iseuqal(C{k}, test_matrix)
index = k;
break;
end
end
Or if you need all occurrences:
index = false(1, numel(C));
for k = 1:numel(C)
if isequal(C{k}, test_matrix)
index(k) = true;
end
end
found = find(index); % Or use the logical index vector directly
A shorter form:
found = find(cellfun(@(c) isequal(c, test_matrix), C));
At least under R2009a, the for loop method is 6 times faster for finding a 10x10 matrix in a 1 x 1000 cell array. cellfun is nice, but slow.

댓글 수: 2

amina KHARBACHE
amina KHARBACHE 2017년 7월 6일
i was lloking for simple insttruction with matlab who find directly the matrix inside the cell matrix
but with a simple loop for can do it
for i=1numel(cellmatrix) if cellmatrix{1,i}==test_matrix save_index=i; break end end
thanx in advance ^^
*************************
i already found it with loop for
thank you so much ^^
Jan
Jan 2017년 7월 6일
편집: Jan 2017년 7월 6일
Do not use == for the comparison, because it works elementwise. It returns a matrix with TRUE for all equal elements, but Matlab's IF expects a scalar. Therefore internally this is evaluated:
if (all(cellmatrix{1,i}(:) == test_matrix(:)) && ~isempy(cellmatrix{1.i})
Better use isequal as in the code I've posted to avoid any ambiguities.
Note: To my surprise this seems to be slightly slower:
index = false(1, numel(C));
for k = 1:numel(C)
index(k) = isequal(C{k}, X);
end
m = find(index);

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

추가 답변 (1개)

Rik
Rik 2017년 7월 6일

0 개 추천

Have you tried ismember?

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2017년 7월 6일

편집:

Jan
2017년 7월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by