How can i iteratively reduce the value of particular matrix elements?
2 views (last 30 days)
Show older comments
Stephanie Diaz
on 9 Nov 2017
Commented: Stephanie Diaz
on 9 Nov 2017
I have a matrix called "weighted_cost" that is created by multiplying two other matrices of the same size, "weighted_C" and "weighted_B". I want to select 4000 random elements from "weighted_cost", and find their indices. Then I want to subtract .05 from all of these elements in another matrix "C", which is the same size as "weighted_cost", if they meet certain conditions. The elements must not have negative values and I try to implement this in the code below. This all should happen 336 times. I do not receive any errors when I run the code, however, upon checking if the original matrix "C" is different from the resulting matrix "C2", I find that they are exactly the same. If the code "works", some cells in matrix "C2" should have much lower values than "C". Can anyone please let me know if anything seems wrong with the code below?
[C,R3]=arcgridread('march_evi_s1_projected.txt');
for t=1:336; %1ST FOR
C2=C
weighted_C=C2.*.4;
weighted_B=B.*.6;
weighted_cost=weighted_C.*weighted_B ;
if any(weighted_cost)<=0
weighted_cost(weighted_cost<0)=0
end
random_patches=weighted_cost(randperm(numel(weighted_cost),4000));
[L1,random_patches_indexes]=ismember(random_patches,weighted_cost);
%Here we are subtracting a certain .05 the EVI of the random cells
if C2(random_patches_indexes)<=0;
C2(random_patches_indexes)=0;
else C2(random_patches_indexes)~=0 & (C2(random_patches_indexes)-.05>0);
C2(random_patches_indexes)-.05;
end
end
0 Comments
Accepted Answer
Roger Stafford
on 9 Nov 2017
Everything that happens in each trip through your for-loop depends on the matrices B and C, and yet B and C remain unchanged. That means each trip through the loop performs exactly the same computations. There is no cumulative effect. If on these trips C2(random_patches_indexes)<=0 is empty, then your resultant C2 will always be equal to C. Note that the line in the “else” part
C2(random_patches_indexes)-.05;
accomplishes nothing. I think you meant
C2(random_patches_indexes) = C2(random_patches_indexes)-.05;
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!