for loop problem using variable

조회 수: 7 (최근 30일)
frankenberry
frankenberry 2018년 6월 25일
댓글: Stephen23 2020년 3월 26일
SADFA
  댓글 수: 1
Stephen23
Stephen23 2020년 3월 26일
Original question from Google Cache:
"for loop problem using variable"
In the code below, the size of scaled_stim and ShpModNoise are [4 32000]. The first line of the code works correctly. Now I want to pass the values to a variable to hold the 4 std (i.e., one std for each row of data). Then I want to average the four stds and adjust the dB levels.
scaled_stim(i,:) = ShpdModNoise(i,:).*10^(-16/20); %this line works, scaled_stim and ShpdModNoise are 4 32000
for scaled_stim(i,:)
adj_scale(i) = 20*log10(std(scaled_stim(i,:))) %converts to dB and stores value in adj_scale
end
adj_out=mean(adj_scale); %takes the average of the 4 values
Questions: 1. why won't the for loop work, each value can output on its own when I enter 1,: or 2,: or 3,: or 4,: - then I get the means at -31.7829; -33.7393; -36.8493; and -39.0284 but when it's in the loop it won't work or store the values. 2. How do I apply adj_out to adjust the data? I write the data out to a sound file after this as follows:
modFreqsstr = num2str(modFreqs(i));
filenam = [pth 'SAM_Noise' modFreqsstr 'x_24.wav'];
audiowrite(filenam,scaled_stim(i,:)',Fs,'BitsPerSample',24)

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

답변 (1개)

Ankita Bansal
Ankita Bansal 2018년 6월 25일
Hi, you are making mistake in line: "for scaled_stim(i,:)"
You need to modify the code as
ShpdModNoise=randi(5,4,32000);
scaled_stim = ShpdModNoise.*10^(-16/20);
for i=1:4
adj_scale(i) = 20*log10(std(scaled_stim(i,:)));
end
adj_out=mean(adj_scale);
I didn't get your 2nd question. Where are you applying adj_out and which data do you want to adjust? Can you provide more information?

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by