How to include the value of a function into a variable?

Hello all,
I have a function with a certail value. Let consider an example :
fix(u/l_segment) = 10
And what I want to do is to include the value of this function into the variables that I am going to declare. Something like this :
Coefficient_[fix(u/l_segment)] = 1 ; (so in this case my variable would become Coefficient_10)
Coefficient_[fix(u/l_segment) - 1] = 1 ; (so in this case my variable would become Coefficient_9)
And so on...
Any idea how can I do this?
Thank you in advance!

3 件のコメント

Stephen23
Stephen23 2021 年 5 月 20 日
編集済み: Stephen23 2021 年 5 月 20 日
Forcing meta-data into variable names is one way that beginners force themselves into writing slow, inefficient, complex code.
Is there any particular reason why you cannot use indexing and/or simply store the meta-data in its own array?
Is there any reason why you cannot use arrays/matrices/vectors (which is exactly how MATLAB is designed to be used)?
Konstantinos Kotsis
Konstantinos Kotsis 2021 年 5 月 23 日
Hello Stephen,
Thank you for your comment.
Well, if I understand your question correctly: In my case and for my system, fix(u/l_segment) will take values from 1 to 1417. In every instant however, I will be only studying/referring to the components starting from [fix(u/l_segment)] till [fix(u/l_segment) - 9]. And these components can be anywhere between 1-1417, depending on the result I get from fix(u/l_segment) for that point in time.
So as you can understand, the variables that I'll be considering for my system, depend on the value of fix(u/l_segment) and that is why I need to consider the value of fix(u/l_segment) and use it as an indicator to understand what component number am I dealing with/working on.
I hope this clears out things a little bit.
If you have any suggestions or recommendations, I'd be happy to hear them.
Stephen23
Stephen23 2021 年 5 月 24 日
編集済み: Stephen23 2021 年 5 月 24 日
"I hope this clears out things a little bit."
Yes, it makes it quite clear that dynamically naming variables would be a pointlessly complex and inefficient approach.
"If you have any suggestions or recommendations"
Exactly as I wrote in my previous comment you should use indexing, possibly storing that meta-data in its own array.

サインインしてコメントする。

回答 (1 件)

Steven Lord
Steven Lord 2021 年 5 月 20 日

0 投票

Can you do this? Yes.
Should you do this? The general consensus is no.

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2021 年 5 月 20 日

編集済み:

2021 年 5 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by