Given an element of a matrix, How to print all the arrays containing that element?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Jan
le 11 Mar 2019
And what is the wanted output for x=4? Is it wanted, that the mask is not symmetric?
Réponses (1)
Stephen23
le 11 Mar 2019
Modifié(e) : Stephen23
le 11 Mar 2019
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 commentaires
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!