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.

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 10월 9일
편집: Andrei Bobrov 2012년 10월 9일

3 개 추천

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

Daniel Shub
Daniel Shub 2012년 10월 9일
You beat me. Nice use of reshape over mean(mean()).
Ali
Ali 2012년 10월 9일
very smart solution. thank you very much

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Daniel Shub
Daniel Shub 2012년 10월 9일

2 개 추천

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);

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

Ali
2012년 10월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by