필터 지우기
필터 지우기

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