How can I avoid using FOR loops and the FIND command for multidimensional array indexing?
显示 更早的评论
I would like to manipulate elements of multidimensional arrays. When working with large arrays, both using FOR loops and avoiding FOR loops by using the FIND function are computationally slow.
As an example, consider the following:
Suppose A is a 10x10x10 matrix as follows:
A=rand(10,10,10);
A(1,2,2)=2;
A(3,4,1)=2;
A(5,6,3)=2;
I would like to set the array elements whose value is 2 to zero. I avoid using FOR loops and use instead the FIND and IND2SUB commands:
I=find(A==2);
[u,v,w]=ind2sub(size(A),I);
A(u,v,w)=0;
For large data arrays, however, this method is computationally slow.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!