3D matrix subtraction

16 Ansichten (letzte 30 Tage)
Thishan Dharshana
Thishan Dharshana am 12 Feb. 2023
Bearbeitet: Thishan Dharshana am 12 Feb. 2023
I have the following loop. The problem is I cannot get different values for NSSST as i and j changes
clear i j
for i=1:12
for j=1:12:324
NSSST=SST(:,:,j)-SSST(:,:,i);
j=j+1;
end
i=i+1;
end
end

Akzeptierte Antwort

Image Analyst
Image Analyst am 12 Feb. 2023
First of all, the clear is not necessary at all. Secondly you don't need to increment i and j because the for loops do that automatically for you. Third, you're not indexing NSSST so you're just overwriting a scalar every iteration. Maybe you want:
for i = 1 : 12
for j = 1 : 12 : 324
NSSST(i, j) = SST(:,:,j) - SSST(:,:,i);
end
end
  2 Kommentare
Torsten
Torsten am 12 Feb. 2023
Bearbeitet: Torsten am 12 Feb. 2023
But you don't get scalars, but 2d matrices as results from the subtraction ...
Maybe something like
count = 0;
for i = 1 : 12
for j = 1 : 12 : 324
count = count + 1;
NSSST(:,:,count) = SST(:,:,j) - SSST(:,:,i);
end
end
Thishan Dharshana
Thishan Dharshana am 12 Feb. 2023
Bearbeitet: Thishan Dharshana am 12 Feb. 2023
Thanks a lot. The answer with "count" is what I wanted.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by