My Matlab for loop wont output the matrix I need

Click For Summary

Discussion Overview

The discussion revolves around a Matlab coding issue related to nested "for" and "if" loops. The participant is attempting to create a matrix that combines a binary signal with a cyclic one, specifically focusing on how to correctly populate the matrix based on certain conditions.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant describes their goal of creating a matrix with amplitude conditions and the sum of data points based on specific criteria involving a binary signal and an analog cyclic signal.
  • The same participant notes that the second column of their output matrix contains identical values across rows, which they believe should vary.
  • Another participant suggests that the issue may stem from not re-initializing the prob1 array before each iteration of the inner loop, which could lead to retaining values from previous iterations.
  • This second participant also proposes an alternative approach that might eliminate the need for the if statement and suggests the possibility of vectorizing the expression to avoid loops altogether.

Areas of Agreement / Disagreement

There is no explicit consensus on the best approach, but one participant expresses gratitude for the suggestions provided, indicating that they found the advice helpful. However, the discussion does not resolve the broader question of the optimal coding strategy.

Contextual Notes

Participants did not provide detailed information on the definitions of variables or the structure of the data being used, which may affect the understanding of the code's functionality.

mcquaya
Messages
2
Reaction score
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
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: 544
Thank you that was very helpful! It's working great now.
 
mcquaya said:
Thank you that was very helpful! It's working great now.

No worries.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K