- #1
dpsguy
- 69
- 0
Could someone please tell me how to make the following code in MATLAB more efficient? I am particularly keen to remove the for loop, as I understand that vectorizing the loop makes the program run much faster.
Here ret and len are positive integers and pm is a number between 0 and 1. X is a matrix of dimensions (ret x len) and is filled with 0s and 1s at random. Basically what this piece of code does is meant to do is generate a random number between 0 and 1 for each element of the matrix X and check if the generated number is less than pm. If it is, then it replaces that element in the matrix with its ones complement.
Any help would be appreciated.
R=rand(ret,len);
[i j]=find(R<pm);
N=horzcat(i,j);
for i=1:length(N)
X(N(i,1),N(i,2))=bitcmp(X(N(i,1),N(i,2)),1);
end
Here ret and len are positive integers and pm is a number between 0 and 1. X is a matrix of dimensions (ret x len) and is filled with 0s and 1s at random. Basically what this piece of code does is meant to do is generate a random number between 0 and 1 for each element of the matrix X and check if the generated number is less than pm. If it is, then it replaces that element in the matrix with its ones complement.
Any help would be appreciated.