A question personally addressing me, probably I must write an answer :)
Instead of implementing all 4 cases, I wrote a generic case. As you already noticed, the 4 cases can be produced using rot90
(rot90(X,0)
does nothing).
To get the indices, I created a meshgrid which contains row- and columnnumbers. Simply put it through the same process of rot90
and diag
, to see which index was moved to the position.
Finally, the outer loop (for r = 1:5
) simply repeats everything.
A = [5,16,18,4,9;
9,10,14,3,18;
2,7,9,11,21;
3,7,2,19,22;
4,9,10,13,8];
[col,row]=meshgrid(1:size(A,1));
x=[10,9,19];
% x=[11,2,9];
% x=[19,9,10];
% x=[3,7,14,4];
for diags = -5:5
for direction=0:3
loc = strfind(diag(rot90(A,direction),diags)', x);
if ~isempty(loc)
colT=diag(rot90(col,direction),diags);
rowT=diag(rot90(row,direction),diags);
rowOfFirstNum=rowT(loc)
columnOfFirstNum=colT(loc)
switch direction
case 0
%code for downRight
case 1
%code for downLeft
case 2
%code for upLeft
case 3
%code for upRight
end
end
end
end