A very basic question about nested for loops

Hello all,
There are 2 persons, which each are pronouncing 9 vowels 10 times (I have in total 2*9*10 sound files). I've derived 2 features (F1 and F2) for each repetition of each vowel for all speakers, and now Im trying to compute mean and covariance for them . Here is where Im stuck: The sigma_est should result in 9 matrices of size 2x2 (as there are 9 vowels) , while it is just of size 2x2. It seems that the outset loop doesn't work properly to make 9 iterations of inner loops. What should I do in order to make it done? Thanks !
Here is the code:
Nvow = 9; % number of vowels
Nrep = 10; % number of repetition for each vowel
Nspeak = 2; % number of speakers in the group
for i=1:Nvow
for k=1:Nspeak
for j=1:Nrep
F1(i,k,j) = features_tst((k-1)*Nvow*Nrep+(i-1)*Nrep+j).value(1);
F2(i,k,j) = features_tst((k-1)*Nvow*Nrep+(i-1)*Nrep+j).value(2);
end
end
F11=reshape(squeeze(F1(i,:,:)),20,1);
F22=reshape(squeeze(F2(i,:,:)),20,1);
mu_est_F1 = sum(F11)/(Nrep*Nspeak);
mu_est_F2 = sum(F22)/(Nrep*Nspeak);
F=[F11 F22];
mu_est=[mu_est_F1 mu_est_F2];
sigma_est=cov(F);
end

 채택된 답변

sixwwwwww
sixwwwwww 2013년 10월 26일

0 개 추천

Dear Negar, re-write the last line as follows:
sigma_est{i} = cov(F);
Then sigma_est will contain 9 matrices of 2x2 size in a cell array. I hope it helps. Good luck!

댓글 수: 4

Negar
Negar 2013년 10월 26일
편집: Negar 2013년 10월 26일
Thank you sir ! It is working beautifully now, but Im wondering how I can learn about the indexing of the variables in the loop.. I mean when to write x(i,:), and when x(i) and when x{i} etc.. I was struggling with this problem the whole afternoon... Is there any references I can learn from? Thanks again
sixwwwwww
sixwwwwww 2013년 10월 26일
You are welcome Negar. You can use x(i,:) when you are indexing a matrix for example. x(i, :) will mean all columns of ith row and i is can be controlled by loop variable. x(i) is used for indexing a row or column vector. Since row or column vector has values in one direction(better to say in one dimension) so x(i) will mean ith element of that column or row vector. x{i} is also used for indexing a row or column vector but here in this case row or column vector has elements in the form of cell and so it is called cell array. You can read more about these in the following links:
I hope it helps. Good luck!
Negar
Negar 2013년 10월 27일
Thank you so much, it really helped :)))
sixwwwwww
sixwwwwww 2013년 10월 27일
You are welcome

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

추가 답변 (1개)

Bhavini Sarkar
Bhavini Sarkar 2016년 9월 17일

1 개 추천

Hi Nagar, I also faced similar problem while using a nested for loop in a program . This is a very common mistake which is hard to debug.

카테고리

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

태그

질문:

2013년 10월 26일

답변:

2016년 9월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by