Filter löschen
Filter löschen

Increasing efficiency of for loop cycling through 2 vectors

1 Ansicht (letzte 30 Tage)
Stephanie Diaz
Stephanie Diaz am 26 Nov. 2017
Bearbeitet: dpb am 28 Nov. 2017
I have code which decreases the value of certain cells in a matrix by a specific value every iteration for 336 iterations. In order to find the indices of the cells that must be decreased, I first determine the unique values in the vector "random_patches", and how many cells with these unique values ("counts") must be decreased. Then I must find out the indices of ALL of the cells with the unique values from the matrix "weighted_cost2", and select x random indices as determined by the corresponding value in "counts". Below is the code that I am using for this. The problem that I am having is that this section of the code is extremely slow, probably because I am cycling through each of the values in vector "edges", selecting x random indices as determined in vector "count", and then joining these indices to the rest in "random_patches_indexes". Does anyone know of a more efficient way of doing what I described? The code below must be iterated through 336 times.
edges = unique(random_patches);
counts = histc(random_patches(:), edges);
random_patches_indexes=0;
for d=1:length(edges);
edges_count=find(weighted_cost2==(edges(d)));
edges_indexes=edges_count(randperm(numel(edges_count), counts(d)));
random_patches_indexes=[random_patches_indexes edges_indexes'];
end
  3 Kommentare
Stephanie Diaz
Stephanie Diaz am 27 Nov. 2017
Thanks @dpb, one question though, what do you mean by a counter?
dpb
dpb am 27 Nov. 2017
A counter is just a variable used for bookkeeping. In this case you need the running total of the next location to be written into the array in order to populate an existing array rather than dynamically reallocating every pass

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 27 Nov. 2017
Bearbeitet: dpb am 27 Nov. 2017
I did find a few minutes -- here's one approach that may or may not actually be quicker; I didn't have time for timing exercises and all...I used shorter variable names; I have trouble keep track of long names when they don't mean anything to me :) ...
[u,~,ir]=unique(R); % find the unique values and indices in R
nu=accumarray(ir,1); % replace histc overhead w/ accumarray for counts
ixW2=arrayfun(@(r) find(W2==r),u,'uniform',0); % the index array in the second array
rndnP=cellfun(@(ix,n) ix(randperm(numel(ix),n)),ixW2,num2cell(nu),'uniform',0); % your random_patches_indexes
The above returns a cell array of numel(u) elements, each of which is the associated array of randomized indices from the corresponding value located in the weighted_cost2 array (that I called W2 for brevity) and of course R is your random_patches.
Depending on the next step, it may be as convenient to leave them as cell array and continue down the path outlined above of using cellfun to process each. Alternatively, you can convert back to the vector representation of concatenating them all into the vector as you had by
rndnP=cell2mat(rndnP);
  5 Kommentare
Stephanie Diaz
Stephanie Diaz am 27 Nov. 2017
Nevermind, this works! Thank you!
dpb
dpb am 27 Nov. 2017
Bearbeitet: dpb am 28 Nov. 2017
ix and r are the argument dummy variable in the anonymous functions evaluated by arrayfun and cellfun, respectively (as well as n in the latter). They have scope only within the anonymous functions; they are where the array/cell argument values are passed into the function for evaluation. Thus, r is u(i) for each array value of u in turn or the value of the match in the find call; similarly ix and n are the values of the cell variables ixW2 and nu. NB: the cast of the array nu to the cell array so it could be used in the cellfun argument list.
See and read up on arrayfun and cellfun in the documentation for each; they are essentially the same thing excepting one operates on the elements of an array one-at-a-time while the other works on the contents of a cell array, one-cell-at-a-time. One has to use cell arrays because there won't be the same number of elements so arrays won't work to hold more than one set of values at a time.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by