Tuesday, 15 June 2010

matlab - How to change the value of a random subset of elements in a matrix without using a loop? -


I am currently trying to select a subset of 0 in very large metrics (about 400x300 elements) 1. I am able to do this, but it is necessary to use a loop in it, where in each instance it selects the next value in a random length vector. In other words, 50% of the 0 in the matrix were randomly selected, change in one-to-one-time, and 1:

  z = 1; For z = 1: (.5 * Number oopharyzes) A ​​(zeropodia (rpnmopheroza (z), 1), zoropation (rpnmopherio (z), 2)) = 1; Z = z + 1; Where 'A' is the matrix, 'zyopis' matrix has 2-column wide matrix with conditions of 0 (if you want "coordinate"), and 'rpnumberofzeros' one The randperm vector is the number of zero in the matrix from 1.  

For example, for z = 20, the code might be something like this:

A (3557, 2684) = 1;

... so that the 0 that appears in this position in 0 is now 1.

It does this loop thousands of times because the .5 * number is very large number, it essentially takes a long time, so my question can be done without a loop? Or at least, the way the processing takes less time / processing?

As I said, only one thing that needs to be done is changed to 1% of the 50% (or whatever ratio) of 0% is a completely random selection.

Thanks in advance for help, and I know that I can clean anything! I am new here, so apologies in advance if I have made a wrong way.

This is very easy. I want to present you with my friend sub2ind You can take the row and column coordinates of a matrix and convert them to the linear column-major index so that you can convert one of the matrix to the same call With the use of many values. As such, you want the equivalent code:

 % // First use values ​​in rpnumberofzeroes vals = rpnumberofzeroes (1: 0.5 * numberofzeroes, :); % // Now, we use columns of these to set rows and columns, we want to reach% // one lines = jeopardy (vals (:, 1), 1); Cols = zeroposition (Wall (:, 2), 2); Get% // linear index through sub-left ind 1 = all 2 id (size (A), rows, columns); % // Now set this position in 1A (IND 1) = 1;  

The first statement gets synchronized stored in rpnumberofzeroes in the first part of your matrix. The first column is row coordinates, the second column is the column coordinates. Note that in your code, you want to use the value in zeroposition to access the locations located in A , such as the correct zeroposition To know the rows and columns, remove the rows and columns related to rpnumberofzeroes . Once we do this, we will remove these new lines and columns from zeroposition and index A . sub2ind requires three inputs - the size of the matrix that you are trying to access ... in our case, this is the a , line coordinates and column coordinates . Output, column is a set of major indices, which is counted for each row and column pair.

The last piece of the puzzle is to use it in A in the index and sets it to 1 position.


No comments:

Post a Comment