My Matlab for loop wont output the matrix I need

In summary: It's always a good idea to reinitialize variables if you're unsure of their initialization; some languages do it for you, some don't. I learned programming on languages that didn't, so it's automatic for me.
  • #1
mcquaya
2
0
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
 
Physics news on Phys.org
  • #2
mcquaya said:
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
 

Attachments

  • phys - 12 06 04 matlab loop 01.jpg
    phys - 12 06 04 matlab loop 01.jpg
    33.7 KB · Views: 455
  • #3
Thank you that was very helpful! It's working great now.
 
  • #4
mcquaya said:
Thank you that was very helpful! It's working great now.

No worries.
 
  • #5



It's difficult to provide a specific solution without seeing the full code and understanding the purpose of the nested for and if loops. However, here are some general suggestions that may help:

1. Check your variable assignments: Make sure that the variables you are using in the for loop are properly assigned and updated within the loop. For example, in your code, it seems like the variable "prob1" is not being updated within the loop, which could be causing the issue in the second column.

2. Debug your code: Use the debugger in Matlab to step through your code and see how the values of your variables are changing at each iteration of the loop. This will help you identify where the issue is occurring and make necessary changes.

3. Use vectorization: Instead of using nested for loops, consider using vectorized operations in Matlab. This can often be more efficient and less prone to errors. You can read more about vectorization in Matlab's documentation.

4. Check your logic: Make sure that the logic in your if statement is correct and is giving you the desired outcome. You can also try using a different approach to achieve the same result.

5. Seek help: If you are still unable to figure out the issue, consider seeking help from a colleague or posting your code on a forum to get feedback from other Matlab users.

Overall, it's important to thoroughly test and debug your code to identify and fix any issues that may be causing incorrect output.
 

1. Why isn't my for loop outputting the expected matrix?

There could be several reasons for this. Check to make sure your for loop is properly structured and that you are using the correct syntax for your desired output. Additionally, make sure your loop is not encountering any errors or infinite loops.

2. How can I troubleshoot my for loop to find the issue?

One way to troubleshoot your for loop is to use the "disp" function to display the values of variables at different stages of the loop. This can help you identify any errors or unexpected results. You can also try running your loop with a smaller dataset to see if the issue persists.

3. What can I do if my for loop is taking a long time to output the matrix?

If your for loop is taking a long time to output the matrix, it could be due to inefficient code or a large dataset. Consider optimizing your code or breaking the loop into smaller chunks to improve performance.

4. Can using nested for loops affect the output of my matrix?

Yes, nested for loops can affect the output of your matrix if they are not properly structured. Make sure to use the correct indices and update them accordingly to avoid any unexpected results.

5. Is there a more efficient way to achieve the same result as my for loop?

Depending on your specific task, there may be other approaches to achieve the same result as a for loop. For example, you could use vectorization or built-in functions in Matlab to perform operations on arrays. It is always a good idea to explore different methods and choose the most efficient one for your specific task.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
848
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top