New Reply

My Matlab for loop wont output the matrix I need

 
Share Thread Thread Tools
May22-12, 08:42 PM   #1
 

My Matlab for loop wont output the matrix I need


I'm somewhat new to Matlab and am having issues with a nested "for" and "if" loop. My code is below. This code is combining a binary signal with a cyclic one. I'm trying to create a matrix with the amplitude condition in the first column and in the second column I want the sum of the number of data points where the binary signal reads "1" and the analog cyclic signal's value is above the amplitude condition. I know the if loop works like I want because when I isolate it it gives me the results I want.

The problem is with the for loop. The second column has all the same number when I know they should vary. Every value is equal to what the correct value is for the first row. After the first row every number is the same for some reason.

n=length(stab(:,1));
prob=zeros(2,2);
prob1=zeros(n,1)

for k=1:100
a(k)=k;
for i=1:n
if ((z2(i)>=a(k))||(z2(i)<=-a(k))) && (stab2(i)==1)
prob1(i)=1;
end
prob(k,2)=sum(prob1);
end
prob(:,1)=a;
end
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Bird's playlist could signal mental strengths and weaknesses
>> Minus environment, patterns still emerge: Computational study tracks E. coli cells' regulatory mechanisms
>> Bacterium uses natural 'thermometer' to trigger diarrheal disease, scientists find
Jun4-12, 10:04 AM   #2
 
Quote by mcquaya View Post
I'm somewhat new to Matlab and am having issues with a nested "for" and "if" loop. My code is below. This code is combining a binary signal with a cyclic one. I'm trying to create a matrix with the amplitude condition in the first column and in the second column I want the sum of the number of data points where the binary signal reads "1" and the analog cyclic signal's value is above the amplitude condition. I know the if loop works like I want because when I isolate it it gives me the results I want.

The problem is with the for loop. The second column has all the same number when I know they should vary. Every value is equal to what the correct value is for the first row. After the first row every number is the same for some reason.

n=length(stab(:,1));
prob=zeros(2,2);
prob1=zeros(n,1)

for k=1:100
a(k)=k;
for i=1:n
if ((z2(i)>=a(k))||(z2(i)<=-a(k))) && (stab2(i)==1)
prob1(i)=1;
end
prob(k,2)=sum(prob1);
end
prob(:,1)=a;
end
I think (provided I've understood the intent of your code) that your problem is that you don't re-initialize prob1 before every 'i' loop; consequently, it retains the values from the first iteration and, given that a(k) will have lowest 'a' value it will have the highest number of 1s. Setting prob1 to all zeros before the loop should cure the problem.

I'm not a Matlab user, but I think you may be able to do away with the if statement and direct assign the result of your conditional expression to a variable; this would avoid having to reset prob1. I've attached a Mathcad image that shows this method. You may even be able to vectorize the entire expression and avoid the loop altogether.

NR
Attached Thumbnails
phys - 12 06 04 matlab loop 01.jpg  
Jun4-12, 10:20 AM   #3
 
Thank you that was very helpful! It's working great now.
Jun6-12, 06:52 AM   #4
 

My Matlab for loop wont output the matrix I need


Quote by mcquaya View Post
Thank you that was very helpful! It's working great now.
No worries.
New Reply

Tags
for loop, matlab, programing
Thread Tools


Similar Threads for: My Matlab for loop wont output the matrix I need
Thread Forum Replies
Is it safe to loop the inverter's output neutral to loop with the mains neutal?? Electrical Engineering 1
Mathematica storing output generated from a loop Math & Science Software 6
Matrix Output (Matlab) Math & Science Software 1
MatLab: How to add a matrix to a structure without a loop? Math & Science Software 5
Matlab help. Can't figure out why this wont work! Math & Science Software 6