How can i iteratively reduce the value of particular matrix elements?

3 次查看(过去 30 天)
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

采纳的回答

Roger Stafford
Roger Stafford 2017-11-9
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;
  1 个评论
Stephanie Diaz
Stephanie Diaz 2017-11-9
Hi Roger, I think I see where I went wrong. The " if C2(random_patches_indexes)<=0" portion was meant to set all cells/indices with a negative value to zero. If the particular indices weren't 0, then I wanted their value to decrease by .05 I now see that if the matrix meets the first condition, the code won't evaluate the corresponding "else" statement, correct?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by