取消
显示结果 
搜索替代 
您的意思是: 

Creating arrays in a nested for loop

已解决!
转到解答

Hi all. 


I have been stuck in this problem for quite a while now and I still don't know how to continue. Some outside input would be greatly appreciated!! 

I'm trying to do the following: 

- Take an array of numbers, check if they are inside of a certain range (ex. between 2 and 4)

- Build two new arrays: one with all the numbers that are inside the range, and one with the rest. 

An additional condition is that the amount and value of range conditions will change (for example, it could be between 2 and 4 only/  2 and 4 AND 6 and 7 depending on the input)

To process this, I created two nested for loops - one that goes over an array that includes the range conditions and the inside one that actually goes and checks if the values are within the range. I think I've done this part successfully, but the next part is confusing to me - how do I actually create the separate arrays within the two for loops?


My apologies if I did not explain this well. Another method of conveying this problem is that I want to translate the following into Labview: 

Array ranges   // ex. [1 5 7 10] -> this means we want to split numbers based on those which are in (1,2) and (4,5) vs. those that aren't

Array values  // [2 3 6 11 3] 

Array inValues // Array for values inside range
Array outValues // Array for values outside range

 

for m = 1:size(ranges)

             for n = 1:size(values)
                           if (THE NUMBER IS INSIDE EITHER RANGE)
                                          inValues = [inValues  NEWNUMBER] 
                           else
                                         outValues = [outValues NEWNUMBER] 
                           end

              end
end

Seems easy enough, but it gets so chaotic with Labview that I don't know what to do. I've attached a picture for reference -  the insert into array function doesn't actually add into the array, it creates a new array. How can I save that up for when the nested for loop ends? 

 


I've seen some examples with shift registers that are within one loop and I could not successfully transfer that into my block diagram. 

Any help/direction would be greatly appreciated. 
 

0 项奖励
1 条消息(共 7 条)
5,832 次查看
解答
接受人 jylee55

 Hi!

 

See the following example to understand how you can create an array in For Loop 🙂

 

Arrays in for loop.png

In your case you can also uce Conditional terminal to create an array only when then numbers are within range. It makes the code much cleaner than case structures 🙂

 

Hope this helps, if not, let me know so I can help you!

 

Marcin

 

              

Think Dataflow - blog.mtapp.tech
0 项奖励
2 条消息(共 7 条)
5,774 次查看

@jylee55 wrote:

Array ranges   // ex. [1 5 7 10] -> this means we want to split numbers based on those which are in (1,2) and (4,5) vs. those that aren't

 


A few comments.  First, I am totally mystified by your "Array ranges".  I see almost no relationship between the example numbers [1 5 7 10] and the resulting criteria, numbers in the range (1, 2) and (4, 5).

 

More important is the need to be very careful when talking about "in range", where you need to specify (carefully!) whether you are dealing with integers (which permit "equality" comparisons) or floats (which can "look the same" but fail to be "equal", for example, a representation of "0.1" and the result of 1 divided by 10).

 

A second is a "math thing" -- a convention is that writing [1, 2] means "numbers between 1 and 2, including the end points", while (1, 2) means "numbers between 1 and 2, excluding the end points".  So the integers in (1, 2) are ... the empty set, as there are no integers strictly between 1 and 2.

 

Finally, if you use the In Range function, note that you can choose to include or exclude either (or both) of the end points in your comparison, but be aware of whether you are using it for integers or floats.

 

Bob Schor

0 项奖励
3 条消息(共 7 条)
5,751 次查看

Hi Bob, 

Sorry - [ 1 5 7 10] should lead to (1, 5) and (7, 10). 


I can understand your endpoint concerns from the 'ranges' example that I provided. However, I am not using this code for integers, but certain periods on a waveform. In my specific scenario there should not be any important signals at these end points other than noise. You are right and I should consider the endpoint scenarios more closely, but I want to fix this array problem I am having first. 

0 项奖励
4 条消息(共 7 条)
5,737 次查看

@mtwar 

Hi! Thanks for your input, it works very well with finding an Array of numbers within that range. However, can I make it such that a second array is displayed which has the rest of the numbers, rather than displaying all of the numbers in that set? 

Thanks! 

0 项奖励
5 条消息(共 7 条)
5,727 次查看
You can add one more conditional indexing and build array when the condition is false or true as required.
Thanks
uday
0 项奖励
6 条消息(共 7 条)
5,723 次查看

@udka 

I see. I can just at a 'Not' and it will do the false ones. Thanks a lot guys!! 

0 项奖励
7 条消息(共 7 条)
5,720 次查看