Can I assign values to an array in a diagonal direction from a reference point on that array?
Mostrar comentarios más antiguos
If I have an array of size NxN,called X, and I count through it using an (i,j) for loop like this
for i = 1:N
for j = 1:N
and im looking for a value in it like this
if(X(i,j) == 1)
I want to start where I find a 1 and change the diagonal lines around it to an 8. So far ive tried code similar to this and many things like it but it doesnt seem to work how I envision it
for i = 1:N
for j = 1:N
if(X(i,j) == 1)
X(i+1,j+1) = 8;
X(i-1,j-1) = 8;
X(i+1,j-1) = 8;
X(i-1,j+1) = 8;
end
end
end
I want the outcome to be something like
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
this turns into this
8 0 0 0 8
0 8 0 8 0
0 0 1 0 0
0 8 0 8 0
8 0 0 0 8
Respuesta aceptada
Más respuestas (4)
X0=logical(X);
[I,J]=find(X0);
[II,JJ]=ndgrid(1:N);
X=reshape(ismember(II(:)-JJ(:),I-J) + ismember(II(:)+JJ(:),I+J),N,N);
X(X>0)=8;
X(X0)=1;
Guillaume
el 18 de Sept. de 2017
Another option, with no issues near the edges. Requires the image processing toolbox:
se = min(1, eye(5) + fliplr(eye(5))); %The cross. Can be any pattern of 0s and 1s.
newX = 8 * imdilate(X, se); %replace each 1 by the se pattern and multiply by 8
newX(logical(X)) = 1; %set original 1s back to 1
Andrei Bobrov
el 18 de Sept. de 2017
Editada: Andrei Bobrov
el 19 de Sept. de 2017
s = size(X);
[~,d(:,2)] = spdiags(rot90(X));
[~,d(:,1)] = spdiags(X);
out = full(spdiags(ones(s(1),size(d,1)),d(:,1),s(1),s(2)) +...
rot90(spdiags(ones(s(2),size(d,1)),d(:,2),s(2),s(1)),-1));
k = max(out(:));
out(out < k & out > 0) = 8;
out(out == k) = 1;
or
[m,n] = size(X);
ii = find(X);
a = toeplitz(0:-1:1-m,0:n-1);
b = hankel(1:m,m + (0:n-1));
aa = a(ii);
bb = b(ii);
out = ((sum(reshape(aa,1,1,[]) == a,3) + ...
sum(reshape(bb,1,1,[]) == b,3)) > 0) + 7*X;
James Tursa
el 18 de Sept. de 2017
Yet another method:
[M,N] = size(X);
n = min(M,N);
XXX = repmat(X,3,3); % work with a big matrix so we don't have to worry about edge cases
[R,C] = find(X);
for k = 1:numel(R) % replace the diagonal spots with 8's in the big matrix
r = R(k) + M;
c = C(k) + N;
x = sub2ind(size(XXX),r+[-n:-1,1:n],c+[-n:-1,1:n]); % upper left to lower right diagonal
XXX(x) = 8;
x = sub2ind(size(XXX),r+[n:-1:1,-1:-1:-n],c+[-n:-1,1:n]); % lower left to upper right diagonal
XXX(x) = 8;
end
X = XXX(M+1:M+M,N+1:N+N); % extract the "original" matrix from the middle
Works for any rectangular matrix X, and overwrites all 1's that are on a diagonal of other 1's with 8's.
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!