[英]Setting values of a matrix equal to a function of their indices in MATLAB
I'm very new to MATLAB so I am not fully aware of all the tips and tricks available in the language. 我对MATLAB非常陌生,因此我不完全了解该语言提供的所有提示和技巧。 I am trying to create a matrix of values equal to a function of their indicies, I have tackled this in my usual approach, but I feel there may be a much faster way of doing this.
我试图创建一个值矩阵,使其等于其指数的函数,我已经用通常的方法解决了这个问题,但是我觉得这样做的方法可能更快。 Thanks
谢谢
outputArray = zeros(rowCount, columnCount);
for i = 1:rowCount
iComp = ((i - (rowCount / 2) * constPWM / rowCount)^2);
for j = 1:columnCount
jComp = ((j - (columnCount/ 2) * constPWM / columnCount)^2);
outputArray(i,j) = iComp + jComp;
Use vectorization: 使用向量化:
[ii,jj]=meshgrid(1:rowCount,1:columnCount);
outputArray= ((ii - (rowCount / 2) * constPWM / rowCount)^2)+...
((jj - (columnCount/ 2) * constPWM / columnCount)^2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.