If I have a 3d matrix(A), how can i check if a given 2d matrix(B) is one of matrix A's pages?
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a 3d matrix A, let say A is a 2x2x3 matrix as follows [1 2 ;3 4] [5 6;3 4] [5 6;1 2]
now I want to know if a 2x2 matrix like [5 6;3 4] is one of A's pages. so since [5 6;3 4] is the second page of A, I should get 1 as the answer.
댓글 수: 0
채택된 답변
Jan
2013년 2월 12일
Another approach:
A = cat(3, [1 2; 3 4], [5 6; 3 4], [5 6; 1 2])
B = [5 6; 3 4]
S = reshape(A, 1, []);
T = reshape(B, 1, []);
match = strfind(S, T);
Result = any(mod(match, 4) == 1);
This can be made a one-liner easily.
추가 답변 (2개)
Teja Muppirala
2013년 2월 12일
Given a 3d matrix A:
A = cat(3, [1 2; 3 4], [5 6; 3 4], [5 6; 1 2])
And some 2d matrix B
B = [5 6; 3 4]
Then, the following expression is one way to see if B is a page in A:
any(all(all(bsxfun(@eq,A,B)))) % Returns true
Moreover, if you would like to know which page of A in which B was found, you could do this:
find(all(all(bsxfun(@eq,A,B)))) % Returns 2
댓글 수: 0
Jan
2013년 2월 12일
편집: Jan
2013년 2월 12일
What about a simple FOR loop?
A = cat(3, [1 2; 3 4], [5 6; 3 4], [5 6; 1 2])
B = [5 6; 3 4]
Result = false;
for iA = 1:size(A, 3)
if isequal(A(:, :, iA), B)
Result = true;
break;
end
end
Here it is exploited, that a single trailing index is omitted automatically:
size(A(:, :, 1))
does not reply [2, 2, 1], but [2, 2]. Otherwise ISEQUAL would fail here.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!