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

6 visualizaciones (últimos 30 días)
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)

Respuesta aceptada

SensingRemotes
SensingRemotes el 27 de Sept. de 2020
Editada: SensingRemotes el 27 de Sept. de 2020
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

Más respuestas (1)

KSSV
KSSV el 21 de Sept. de 2020
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 comentarios
SensingRemotes
SensingRemotes el 27 de Sept. de 2020
Those did not work, but thanks anyways. I figured it out.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by