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
댓글 수: 0
채택된 답변
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;
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!