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

6 ビュー (過去 30 日間)
SensingRemotes
SensingRemotes 2020 年 9 月 21 日
編集済み: SensingRemotes 2020 年 9 月 27 日
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 件のコメント
Image Analyst
Image Analyst 2020 年 9 月 21 日
And the first 3 would be
[R, G, B] = imsplit(NDVI);
SensingRemotes
SensingRemotes 2020 年 9 月 27 日
Those did not work, but thanks anyways. I figured it out.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by