Generate numbered variables from values in array

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

"I would like to create a number of specifically numbered variables so that"
The general consensus on this is that it is not a good idea - TUTORIAL: Why Variables Should Not Be Named Dynamically (eval)
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)?
... I would like to create a number of specifically numbered variables ...
Please do not do that. Just use the indexed variables.
If you want to save the indices of the ‘x’ values above threshold ‘y’ perhaps:
xidx = find(x >= y);
would do what you want.
.
You can't do what you want. Even if you could do what you want, you do not want to do it. This would be a terrible coding style, and would result in your learning bad programming practices, EVEN IF you could manage it.
Instead, learn to use vectors and arrays, how MATLAB was designed to be used. For example, you can do much of what you are asking with a single conv operation applied to a vector of elements. Really, you need to learn how to use MATLAB properly.
thanks, but "find" only does part of what I want.
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.
For context I am importing a video, analysing each frame, and creating an array of values which depending on some operation I do to each frame. So if the video has 100 frames, there will be an array of 100 values.
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 don’t entirely understand what you’re doing. It would be easiest to use a cell array or a structure array to save the desired data, as well as any metadata you want to store with it.
I would still use an aray of some sort, and not numbered variables. Numbered variables are inefficient and next to impossible to work with.
You don't need to generate a uniquely named variable. All you need is to know which one was the problem. That can be gained from tools like find. I'm sorry, but you need to learn to use MATLAB.
Instead, you want to use a language where you write all the rules. No problem. Start writing. Call your own language whatever you want. Or... you can learn the MATLAB syntax. It really is that simple.
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?
"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)

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

Asked:

on 19 Jan 2024

Commented:

on 20 Jan 2024

Community Treasure Hunt

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

Start Hunting!