how to do this in a for loop in Matlab

I have a 3-dimensial matrix W(160,170,18) and wanna to compute the difference between each to sucessive matrices inside W. for example diff1 = W(:,:,1) - W(:,:,2) and diff2 = W(:,:,2) - W(:,:,3), etc ...
Next I want to select some special parts of the resulting matrices, For example:
NewDiff1 = [diff1(20:50,110:140);diff1(60:90,110:140)];
and the same thing for the other matrices. finally I want to compute the mean of each matrix and the error as follow:
mean1 = mean(mean(NewDiff1)); er1 = 0.1-abs(mean1);
I successeded to do this for each matrix alone, but prefer to do all at once in a for loop. Can you please help me to do it.

 Risposta accettata

Andrei Bobrov
Andrei Bobrov il 9 Ott 2012
Modificato: Andrei Bobrov il 9 Ott 2012
dff = diff(W,1,3)
a = dff([20:50,60:90],110:140,:)
meanN = mean(reshape(a,[],size(a,3)));
erN = .1 - abs(meanN);

2 Commenti

You beat me. Nice use of reshape over mean(mean()).
very smart solution. thank you very much

Accedi per commentare.

Più risposte (1)

I am not sure if this is really what you want ...
First create some dummy data
x = randn(160,170,18);
You can create all your "diff" matrices with the diff function.
y = diff(x, 1, 3);
Since they are all 1 matrix, calculating the mean and error is now easy
mu = squeeze(mean(mean(y([20:50, 60:90], 110:140, :))));
er = 0.1-abs(mu);

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by