How to change certain values in a 3 layer matrix and fill in another matrix?

조회 수: 4 (최근 30일)
Hello everyone,
Newbie here. I have a 3 layer matrix that is defined as NDVI which has 7971 rows, 7851 columns, and 3 layers. I am working with blue, red, and green bands from satellite imagery. I want to replace each NDVI value greater than 0, and set it equal to the value of the green band. When NDVI is less than zero, set it equal to the blue band. I then want those replaced values, and the unreplaced values to fill in another matrix called NDVII with the same around of rows, columns, and layers.
Below is what I have so far. I have no experience writing a for loop on Matlab what-so-ever. Thanks for any help/advice.
NDVII=zeros(7971,7851,3);
for NDVII=7971:7851:3
NDVI=NDVII;
if NDVI>0
G=NDVII;
else NDVI<0
B=abs(NDVII);
end
end
imagesc(NDVII)

채택된 답변

SensingRemotes
SensingRemotes 2020년 9월 27일
편집: SensingRemotes 2020년 9월 27일
Here is the code in case anyone runs into this problem in the future:
[numRows, numCols] = size(NDVI); % matrix size
NDVII = zeros(numRows, numCols, 3); % zero filled array called NDVII with 3 layers
for j = 1:numCols % counter 'j' is going through each column
for i = 1:numRows % counter 'i' is going through each row
if NDVI(i, j) > 0
NDVII(i, j, :) = G(i, j); % ':' goes through all 3 layers
NDVII(i, j, 2) = 1; % makes it green
elseif NDVI(i, j) < 0
NDVII(i, j, :) = B(i, j);
NDVII(i, j, 3) = 1; % makes it blue
end
end
end

추가 답변 (1개)

KSSV
KSSV 2020년 9월 21일
Let NDVI be your 7971*7851*3 matrix.
R = NDVI(:,:,1) ;
G = NDVI(:,:,2) ;
B = NDVI(:,:,3) ;
% replace values
% repalce each >0 value with Green values
R(R>0) = G(R>0) ;
B(B>0) = G(B>0) ;
% replace each < 0 value with Belue values
R(R<0) = B(R<0) ;
G(G<0) = B(G<0) ;
%
NDVI(:,:,1) = R ;
NDVI(:,:,2) = G ;
NDVI(:,:,3) = B ;
  댓글 수: 3
SensingRemotes
SensingRemotes 2020년 9월 27일
Those did not work, but thanks anyways. I figured it out.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by