Azzera filtri
Azzera filtri

How to work on "for loop" to save different column variable values to single vector?

1 visualizzazione (ultimi 30 giorni)
My question is=>
i have two variables, Ea and Ed, Ea contains single value and Ed contains 4 values like this,
Ea=10.5
Ed=51.6149 27.1981 13.2580 2.3396
These are wave energy values for 1 second of audio clip. But i have to calculate energy values for 60 seconds. So i have done storing each energy values in different variables like, Ed1, Ed2, Ed3...Ed60. These are Ed values for 60 seconds.
Now the problem i ve got is=>
i want to store all 1st column values of each variable(Ed1,Ed2,Ed3 till Ed60) in one vector(VEd1,VEd2,VEd3 and VEd4), so that i can draw or plot graph to analyze.
Here VEd1 should contain only 1st column values of all Ed values and also VEd2, VEd3 and VEd4.
I have tried "for" loop but could not get how to do tat... If i wont use any loops here, i wont b able to finish this before my dead line as i have to train the module for different audios.
Here is whatever i have tried... but i got errors...
for i=1:60,
VEa=Ea(i);
end
ERROR: Attempted to access Ea(2); index out of bounds because numel(Ea)=1.
then tried=>
for i=1:60,
VEa=Ea.i;
end
ERROR: Attempt to reference field of non-structure array.
Then i satisfied with this=>
for i=1,
VEd1=[Ed1(i) Ed2(i) Ed3(i) Ed4(i) Ed5(i) Ed6(i) Ed7(i) Ed8(i) Ed9(i) Ed10(i) Ed11(i) Ed12(i) Ed13(i) Ed14(i) Ed15(i) Ed16(i) Ed17(i) Ed18(i) Ed19(i) Ed20(i) Ed21(i) Ed22(i) Ed23(i) Ed24(i) Ed25(i) Ed26(i) Ed27(i) Ed28(i) Ed29(i) Ed30(i) Ed31(i) Ed32(i) Ed33(i) Ed34(i) Ed35(i) Ed36(i) Ed37(i) Ed38(i) Ed39(i) Ed40(i) Ed41(i) Ed42(i) Ed43(i) Ed44(i) Ed45(i) Ed46(i) Ed47(i) Ed48(i) Ed49(i) Ed50(i) Ed51(i) Ed52(i) Ed53(i) Ed54(i) Ed55(i) Ed56(i) Ed57(i) Ed58(i) Ed59(i) Ed60(i)];
end
for i=2,
VEd2=[Ed1(i) Ed2(i) Ed3(i) Ed4(i) Ed5(i) Ed6(i) Ed7(i) Ed8(i) Ed9(i) Ed10(i) Ed11(i) Ed12(i) Ed13(i) Ed14(i) Ed15(i) Ed16(i) Ed17(i) Ed18(i) Ed19(i) Ed20(i) Ed21(i) Ed22(i) Ed23(i) Ed24(i) Ed25(i) Ed26(i) Ed27(i) Ed28(i) Ed29(i) Ed30(i) Ed31(i) Ed32(i) Ed33(i) Ed34(i) Ed35(i) Ed36(i) Ed37(i) Ed38(i) Ed39(i) Ed40(i) Ed41(i) Ed42(i) Ed43(i) Ed44(i) Ed45(i) Ed46(i) Ed47(i) Ed48(i) Ed49(i) Ed50(i) Ed51(i) Ed52(i) Ed53(i) Ed54(i) Ed55(i) Ed56(i) Ed57(i) Ed58(i) Ed59(i) Ed60(i)];
end
for i=3,
VEd3=[Ed1(i) Ed2(i) Ed3(i) Ed4(i) Ed5(i) Ed6(i) Ed7(i) Ed8(i) Ed9(i) Ed10(i) Ed11(i) Ed12(i) Ed13(i) Ed14(i) Ed15(i) Ed16(i) Ed17(i) Ed18(i) Ed19(i) Ed20(i) Ed21(i) Ed22(i) Ed23(i) Ed24(i) Ed25(i) Ed26(i) Ed27(i) Ed28(i) Ed29(i) Ed30(i) Ed31(i) Ed32(i) Ed33(i) Ed34(i) Ed35(i) Ed36(i) Ed37(i) Ed38(i) Ed39(i) Ed40(i) Ed41(i) Ed42(i) Ed43(i) Ed44(i) Ed45(i) Ed46(i) Ed47(i) Ed48(i) Ed49(i) Ed50(i) Ed51(i) Ed52(i) Ed53(i) Ed54(i) Ed55(i) Ed56(i) Ed57(i) Ed58(i) Ed59(i) Ed60(i)];
end
for i=4,
VEd4=[Ed1(i) Ed2(i) Ed3(i) Ed4(i) Ed5(i) Ed6(i) Ed7(i) Ed8(i) Ed9(i) Ed10(i) Ed11(i) Ed12(i) Ed13(i) Ed14(i) Ed15(i) Ed16(i) Ed17(i) Ed18(i) Ed19(i) Ed20(i) Ed21(i) Ed22(i) Ed23(i) Ed24(i) Ed25(i) Ed26(i) Ed27(i) Ed28(i) Ed29(i) Ed30(i) Ed31(i) Ed32(i) Ed33(i) Ed34(i) Ed35(i) Ed36(i) Ed37(i) Ed38(i) Ed39(i) Ed40(i) Ed41(i) Ed42(i) Ed43(i) Ed44(i) Ed45(i) Ed46(i) Ed47(i) Ed48(i) Ed49(i) Ed50(i) Ed51(i) Ed52(i) Ed53(i) Ed54(i) Ed55(i) Ed56(i) Ed57(i) Ed58(i) Ed59(i) Ed60(i)];
end
NOW can u plz tel me how to do all this in a single loop?
i want to know how this all variables can be used to store the appropriate values... As in C- language its not happening here. if u know something about this plz help me... Thank you...

Risposta accettata

SL B
SL B il 16 Mag 2013
Modificato: SL B il 16 Mag 2013
Why loop when you can just put it in a matrix? You are saying that Ed1 = [51.6149 27.1981 13.2580 2.3396] (this would be a row, but I'm assuming you have it in columns since you said you want the first value in each column of EdXX)
so assuming Edxx are column vectors:
Ed = [Ed1, Ed2, Ed3,.......,Ed60];
Then
Ved1 = Ed(1,:);
Ved2 = Ed(2,:);
Ved3 = Ed(3,:);
Ved4 = Ed(4,:);
  1 Commento
prasanna patil
prasanna patil il 17 Mag 2013
Now u will understand what i meant clearly.
x= input('loop starts from-> ', 's');
y= input('loop starts from-> ', 's');
for i=x:y,
Tw = 25;
Ts = 10;
alpha = 0.97;
R = [ 300 3700 ];
M = 20;
C = 13;
L = 22;
hamming = @(N)(0.54-0.46*cos(2*pi*[0:N-1].'/(N-1)));
x= input('input the audio file-> ', 's');
[ speech, fs, nbits ] = wavread(x);
[MFCCs,FBEs,frames]= mfcc(speech,fs,Tw,Ts,alpha,hamming, R, M, C, L );
[C,L] = wavedec(MFCCs,4,'sym4');
[Ea,Ed] = wenergy(C,L);
VEa(i)=Ea(1);
VEd1(i)=Ed(1);
VEd2(i)=Ed(2);
VEd3(i)=Ed(3);
VEd4(i)=Ed(4);
end
the output will b like this...
loop starts from-> 1
loop starts from-> 2
input the audio file-> 1.wav
input the audio file-> 2.wav
now i can store energy values for many audios in different variables with a single program. :)

Accedi per commentare.

Più risposte (1)

David Sanchez
David Sanchez il 16 Mag 2013
for i=1:4
VEd(k) = [Ed1(i) Ed2(i) Ed3(i) Ed4(i) Ed5(i) Ed6(i) Ed7(i) Ed8(i) Ed9(i) Ed10(i) Ed11(i) Ed12(i) Ed13(i) Ed14(i) Ed15(i) Ed16(i) Ed17(i) Ed18(i) Ed19(i) Ed20(i) Ed21(i) Ed22(i) Ed23(i) Ed24(i) Ed25(i) Ed26(i) Ed27(i) Ed28(i) Ed29(i) Ed30(i) Ed31(i) Ed32(i) Ed33(i) Ed34(i) Ed35(i) Ed36(i) Ed37(i) Ed38(i) Ed39(i) Ed40(i) Ed41(i) Ed42(i) Ed43(i) Ed44(i) Ed45(i) Ed46(i) Ed47(i) Ed48(i) Ed49(i) Ed50(i) Ed51(i) Ed52(i) Ed53(i) Ed54(i) Ed55(i) Ed56(i) Ed57(i) Ed58(i) Ed59(i) Ed60(i)];
end
  2 Commenti
prasanna patil
prasanna patil il 16 Mag 2013
sir, i am not clear with your answer. How can i store separate 60 values in 4 separate variables?
I tried on your code but i didn't get what i wanted.
As u said that ll process like this...
for i=1:4,
v(i)=i;
end
>> v
v =
1 2 3 4
prasanna patil
prasanna patil il 16 Mag 2013
and tried this also...
for k=1:4,i=1:4,
VEd(k) = [Ed1(i) Ed2(i) Ed3(i) Ed4(i) Ed5(i) Ed6(i) Ed7(i) Ed8(i) Ed9(i) Ed10(i) Ed11(i) Ed12(i) Ed13(i) Ed14(i) Ed15(i) Ed16(i) Ed17(i) Ed18(i) Ed19(i) Ed20(i) Ed21(i) Ed22(i) Ed23(i) Ed24(i) Ed25(i) Ed26(i) Ed27(i) Ed28(i) Ed29(i) Ed30(i) Ed31(i) Ed32(i) Ed33(i) Ed34(i) Ed35(i) Ed36(i) Ed37(i) Ed38(i) Ed39(i) Ed40(i) Ed41(i) Ed42(i) Ed43(i) Ed44(i) Ed45(i) Ed46(i) Ed47(i) Ed48(i) Ed49(i) Ed50(i) Ed51(i) Ed52(i) Ed53(i) Ed54(i) Ed55(i) Ed56(i) Ed57(i) Ed58(i) Ed59(i) Ed60(i)];
end
i =
1 2 3 4
ERROR: In an assignment A(I) = B, the number of elements in B and I must be the same.

Accedi per commentare.

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by