Generate numbered variables from values in array
Show older comments
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
on 19 Jan 2024
"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)?
Star Strider
on 19 Jan 2024
‘... 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.
.
John D'Errico
on 19 Jan 2024
Edited: John D'Errico
on 19 Jan 2024
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.
Bill White
on 19 Jan 2024
Star Strider
on 19 Jan 2024
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.
John D'Errico
on 19 Jan 2024
Edited: John D'Errico
on 19 Jan 2024
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.
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
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.
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)
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]
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
You might want to specially handle the case where the last element of x falls into the "special" category (in this case, >= 16.)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!