如何在MATLAB中使用线性索引为4-D矩阵的对角线赋值? [英] How can I assign a value to the diagonals of a 4-D matrix using linear indexing in MATLAB?

查看:367
本文介绍了如何在MATLAB中使用线性索引为4-D矩阵的对角线赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大小为NxNxPxQ的4-D矩阵A.如何以向量化的方式轻松地将每个NxN二维子矩阵的对角线值更改为1?

I have a 4-D matrix A of size NxNxPxQ. How can I easily change the diagonal values to 1 for each NxN 2-D submatrix in a vectorized way?

推荐答案

结合gnovice的建议,索引元素的一种简单方法是:

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.

这篇关于如何在MATLAB中使用线性索引为4-D矩阵的对角线赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆