Generate numbered variables from values in array

3 views (last 30 days)
I have an array x (of arbitrary length, n) of values equal or greater than zero:
x = (x1, x2, x3, .... xn)
I can find the location (index) of where the values of x are above a certain positive threshold value, y.
When a value in x is greater than the threshold y, I would like to create a number of specifically numbered variables so that
variable_x3 = (x(3))
variable_x50 = (x(50))
where the value of each numbered variable is greater than threshold y. Note that the variable numbers may or may not be sequential.
As an extension, I would like to be able to add the value of immediately adjacent values to the variable; for example
variable_x3 = (x(3-1)) + (x(3)) + (x(3+1))
I can see a problem with
variable_x1 = (x(1)) [ because there is no x(0) to add)
and
variable_xn = (x(n)) [ because there is no x(n+1) to add)
so I need to be able to check for this occurrence and ignore it and just add the adjacent value that exists, i.e.
variable_x1 = (x(1)) + (x(2))
I hope this makes sense. Any help would be appreciated.
  8 Comments
Dyuman Joshi
Dyuman Joshi on 20 Jan 2024
What if adjacent numbers are above threshold?
For e.g 3rd and 4th index are above threshold, what should be the value corresponding to them?
Should it be - v3 = x(2) + x(3) + x(4) and v4 = x(3) + x(4) + x(5)? or something else?
Stephen23
Stephen23 on 20 Jan 2024
"I would like to create a number of specifically numbered variables..."
So you want to force yourself into writing slow, complex, inefficient, obfuscated, buggy code that is hard to debug:
What specifically is stopping you from using the usual simple, neat, easy, clear, efficient indexing?
"Some of these values will be above a certain value, so I need to then create a uniquley named variable which contains the contents of that frame."
I doubt that you actually "need" to do that. Most likely you could use (much better) indexing.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 19 Jan 2024
It tells me where the values are (the index); but I need to go through each index and save the contents of that index to a unique variable which can be exported.
Why do you need a variable with a unique (dynamically generated) name to operate on the data? Let's say you wanted to break this vector up based on where elements whose values are greater than or equal to 16 were located.
x = randperm(20)
x = 1×20
13 1 10 7 19 11 14 6 16 17 2 4 5 9 12 8 18 3 20 15
Find the values where each of those elements are located, and add the "index of the element before the first" and the index of the last element of the array.
f = [0 find(x >= 16) 20]
f = 1×7
0 5 9 10 17 19 20
Now use those indices.
for k = 1:(numel(f)-1)
startInd = f(k)+1; % 1 after the index of the previous data's element that's >= 16
endInd = f(k+1); % The index of the data element >= 16 that ends this data segment
data = x(startInd:endInd)
% Now do whatever you need to do with data
end
data = 1×5
13 1 10 7 19
data = 1×4
11 14 6 16
data = 17
data = 1×7
2 4 5 9 12 8 18
data = 1×2
3 20
data = 15
You might want to specially handle the case where the last element of x falls into the "special" category (in this case, >= 16.)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!