how to do this in a for loop in Matlab

1 visualización (últimos 30 días)
Ali
Ali el 9 de Oct. de 2012
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.

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 9 de Oct. de 2012
Editada: Andrei Bobrov el 9 de Oct. de 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 comentarios
Daniel Shub
Daniel Shub el 9 de Oct. de 2012
You beat me. Nice use of reshape over mean(mean()).
Ali
Ali el 9 de Oct. de 2012
very smart solution. thank you very much

Iniciar sesión para comentar.

Más respuestas (1)

Daniel Shub
Daniel Shub el 9 de Oct. de 2012
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);

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by