필터 지우기
필터 지우기

for loops for two variables (not nested loop)

조회 수: 3 (최근 30일)
UET Hussain
UET Hussain 2017년 11월 30일
편집: UET Hussain 2018년 1월 18일
I've a .dat file with 73280 length. i'm trying to find mean of first 100 values, then mean of 101 to 200 values, then 201 to 300 and so on until last 100 values. 1st error i got was "Index exceeds matrix dimensions." I rounded off the length to 73200 and got rid of this problem.
x = [];
y = rms (a1,100); %a1 is original data
b= length(y);
c= b/100;
for i =1:100:b
x= [mean(y(i:i+99))]
end
subplot (2,2,1); plot (t1, a1)
subplot (2,2,2); plot (x)
subplot (2,2,3); plot (t1,y)
the value of "x" is updating in every iteration. what i want is to store the every value of "x" and then plot it.
  댓글 수: 1
UET Hussain
UET Hussain 2017년 11월 30일
편집: Stephen23 2017년 11월 30일
when I try to apply another for loop,
x = [];
y = rms (a1, 100);
b= length(y);
c= b/100;
for i =1:100:b, j=1:c;
x= [mean(y(i:i+99))]
end
subplot (2,2,1); plot (t1, a1)
subplot (2,2,2); plot (x)
subplot (2,2,3); plot (t1,y)
it actually becomes nested loop, which i dont want

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

채택된 답변

Stephen23
Stephen23 2017년 11월 30일
A loop is not required. Just use reshape. Then you can plot them all at one too!
>> N = 100;
>> V = rand(1,73280); % your data vector
>> Z = N*fix(numel(V)/N);
>> M = mean(reshape(V(1:Z),N,[]),1);
>> plot(M)
  댓글 수: 1
UET Hussain
UET Hussain 2017년 11월 30일
편집: UET Hussain 2018년 1월 18일
Hi Stephen...... thanks for reply... what if i dont know the size of a file (n above case "73280")????? actually i have over over 225 files hence cannot look at each file everytime i'm going to process it.

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

추가 답변 (1개)

M
M 2017년 11월 30일
You can use
x=[x mean(y(i:i+99))];
  댓글 수: 1
UET Hussain
UET Hussain 2017년 11월 30일
i'm using the same thing, Sir.... but cant get the desired result

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

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by