Given an element of a matrix, How to print all the arrays containing that element?
조회 수: 1 (최근 30일)
이전 댓글 표시
Given an element of a matrix, I need to print all the arrays containing that element. So basicaly for each element I need 4 arrays.
Suppose I have the matrix:
A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
If I give input
x = 7
I should get output as:
out1 = 0 9 6 12
out2 = 0 2 11 14
out3 = 0 5 15 0
out4 = 13 10 4 0
Any help will be appreciated. Thank You.
댓글 수: 5
Jan
2019년 3월 11일
And what is the wanted output for x=4? Is it wanted, that the mask is not symmetric?
답변 (1개)
Stephen23
2019년 3월 11일
편집: Stephen23
2019년 3월 11일
This should get you started, adjust as required:
>> A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> N = 7;
>> Rv = {[+0,+0,+0,+0],[-3,-2,-1,+1],[-2,-1,+1,+2],[-2,-1,+1,+2]};
>> Cv = {[-2,-1,+1,+2],[+0,+0,+0,+0],[-2,-1,+1,+2],[+2,+1,-1,-2]};
>> B = zeros(size(A)+5);
>> B(4:end-2,4:end-2) = A;
>> [Rx,Cx] = find(B==N);
>> F = @(r,c)reshape(B(sub2ind(size(B),Rx+r,Cx+c)),1,[]);
>> Z = cellfun(F,Rv,Cv,'Uni',0);
>> Z{:}
ans =
0 9 6 12
ans =
0 2 11 14
ans =
0 5 15 0
ans =
13 10 4 0
You can access the data in the cell array using indexing:
You could probably do something similar using blockproc:
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!