how set diagonal =1 in matrix tridimensional a(:,:,:)

hi,
how can i set=1 the diagonal of the multidimensional matrix
size(COR)
ans =
8 8 188

8 commentaires

Are you looking for the diagonal on each "page", so all 118 layers the same?
Or are you looking for the "space diagonal" -- (1,1,1), (2,2,2), up to (8,8,8) with everything else 0?
aldo
aldo le 20 Juil 2023
hi.
example: given a matrix a(b,c,d) I want to keep all the values ​​of the matrix but set all the diagonals of the sub-matrixes to =1 (b,c)
aldo
aldo le 20 Juil 2023
[r,c,d]=size(a);
for i=1:d
for x=1:r (it's a square...r=c)
a(x,x,i)=1;
end
end
Bruno Luong
Bruno Luong le 20 Juil 2023
Modifié(e) : Bruno Luong le 20 Juil 2023
So you have for-loop that seems correct and arguably preferable to all answers you'll get. Why asking the question?
aldo
aldo le 20 Juil 2023
because i dont want to use cicle for
I thought it could be solved using Eye /diag without using loops
aldo
aldo le 20 Juil 2023
i use the solution of Walter Roberson:
COR1=COR;
COR2=COR;
tic
[r,c,d]=size(COR1);
for i=1:d
for x=1:r
COR1(x,x,i)=1;
end
end
toc
tic
M = repmat(logical(eye(size(COR2,1),size(COR2,2))),1,1,size(COR2,3));
COR2(M) = 1;
toc
fprintf("equal %d \n",isequal(COR1,COR2));
Elapsed time is 0.001945 seconds.
Elapsed time is 0.000182 seconds.
equal 1
Here is the timings of three methods
COR = rand(8,8,188);
timeit(@() methodfor(COR)) % Aldo
ans = 4.0301e-05
timeit(@() methodlogical(COR)) % Walter
ans = 2.3801e-05
timeit(@() methodindex(COR)) % Bruno
ans = 1.6800e-05
function COR = methodfor(COR)
[r,c,d]=size(COR);
for i=1:d
for x=1:r
COR(x,x,i)=1;
end
end
end
function COR = methodlogical(COR)
M = repmat(logical(eye(size(COR,1),size(COR,2))),1,1,size(COR,3));
COR(M) = 1;
end
function COR = methodindex(COR)
[b,c,d] = size(COR);
COR(1+(b+1)*(0:min(b,c)-1)'+b*c*(0:d-1)) = 1;
end
aldo
aldo le 20 Juil 2023
okk thank

Connectez-vous pour commenter.

Réponses (3)

Maybe using eye and repmat
COR = repmat(eye(8),1,1,188);
size(COR)
ans = 1×3
8 8 188

3 commentaires

aldo
aldo le 20 Juil 2023
is not correct
I want to keep all the values ​​of the matrix (except the diagonal)
M = repmat(logical(eye(size(COR,1),sie(COR,2))),1,1,size(COR,3));
COR(M) = 1;
aldo
aldo le 20 Juil 2023
Correct! thank you

Connectez-vous pour commenter.

% Generate dummy test data
a = 0.01*rand(2,3,4)
a =
a(:,:,1) = 0.0058 0.0073 0.0096 0.0027 0.0011 0.0010 a(:,:,2) = 0.0081 0.0020 0.0006 0.0038 0.0068 0.0023 a(:,:,3) = 0.0052 0.0037 0.0065 0.0073 0.0024 0.0026 a(:,:,4) = 0.0057 0.0047 0.0098 0.0098 0.0082 0.0040
[b,c,d] = size(a);
[I,K] = ndgrid(1:min(b,c),1:d);
a(sub2ind([b,c,d],I,I,K)) = 1;
a
a =
a(:,:,1) = 1.0000 0.0073 0.0096 0.0027 1.0000 0.0010 a(:,:,2) = 1.0000 0.0020 0.0006 0.0038 1.0000 0.0023 a(:,:,3) = 1.0000 0.0037 0.0065 0.0073 1.0000 0.0026 a(:,:,4) = 1.0000 0.0047 0.0098 0.0098 1.0000 0.0040
% Generate dummy test data
a = 0.01*rand(2,3,4)
a =
a(:,:,1) = 0.0023 0.0073 0.0085 0.0021 0.0024 0.0021 a(:,:,2) = 0.0073 0.0066 0.0072 0.0021 0.0099 0.0025 a(:,:,3) = 0.0035 0.0094 0.0085 0.0008 0.0069 0.0050 a(:,:,4) = 0.0070 0.0079 0.0051 0.0071 0.0029 0.0022
[b,c,d] = size(a);
a(1+(b+1)*(0:min(b,c)-1)'+b*c*(0:d-1)) = 1;
a
a =
a(:,:,1) = 1.0000 0.0073 0.0085 0.0021 1.0000 0.0021 a(:,:,2) = 1.0000 0.0066 0.0072 0.0021 1.0000 0.0025 a(:,:,3) = 1.0000 0.0094 0.0085 0.0008 1.0000 0.0050 a(:,:,4) = 1.0000 0.0079 0.0051 0.0071 1.0000 0.0022

Question posée :

le 19 Juil 2023

Modifié(e) :

le 20 Juil 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by