How can I assign a value to the diagonals of a 4-D matrix using linear indexing in MATLAB?

前提是你 提交于 2019-12-01 19:27:46

Incorporating gnovice's suggestion, an easy way to index the elements is:

[N,~,P,Q]=size(A);%# get dimensions of your matrix

diagIndex=repmat(logical(eye(N)),[1 1 P Q]);%# get logical indices of the diagonals    
A(diagIndex)=1;%# now index your matrix and set the diagonals to 1.

You can actually do this very simply by directly computing the linear indices for every diagonal element, then setting them to 1:

[N,N,P,Q] = size(A);
diagIndex = cumsum([1:(N+1):N^2; N^2.*ones(P*Q-1,N)]);
A(diagIndex) = 1;

The above example finds the N diagonal indices for the first N-by-N matrix (1:(N+1):N^2). Each subsequent N-by-N matrix (P*Q-1 of them) is offset by N^2 elements from the last, so a matrix of size PQ-1-by-N containing only the value N^2 is appended to the linear indices for the diagonal of the first matrix. When a cumulative sum is performed over each column using the function CUMSUM, the resulting matrix contains the linear indices for all diagonal elements of the 4-D matrix.

You can use direct indexing, and some faffing about with repmat, to add the indexes for a single 50x50 diagonal to the offsets within the larger matrix of each 50x50 block:

Here's an example for a smaller problem:

A = NaN(10,10,5,3);
inner = repmat(sub2ind([10 10], [1:10],[1:10]), 5*3, 10); % diagonals
outer = repmat([10*10 * [0:5*3-1]]', 1, 10*10); % offsets to blocks
diags = inner + outer;
A(diags(:)) = 1;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!