assign numbers to observations

1 回表示 (過去 30 日間)
matla6123
matla6123 2017 年 4 月 5 日
コメント済み: Stephen23 2017 年 4 月 10 日
Hi I have a large dataset involving observations of names in a cell like below.
ABC1
AB56
AC46
AC90
I have another dataset "values" that is made up of doubles, with observations for each of the names listed above.
3x8 doubles
3x8 doubles
6x8 doubles
5x8 doubles
The datasets are specifically ordered so that the third observation in "values" has the third name. I want to give each name a number referring to its position in the original vector. E.g. AC90 is the fourth in the list so everywhere AC90 appears, the number 4 is listed.
  1 件のコメント
Stephen23
Stephen23 2017 年 4 月 10 日
@matla6123: whatever you do, do not try to create/access variable names dynamically. Dynamically accessing variables names is slow, buggy, and obfuscated way to write code:
Much better would be to use a non-scalar structure, as Jan Simon has shown you.

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

採用された回答

Jan
Jan 2017 年 4 月 10 日
If the names, values and their order should be represented, store them e.g. in a struct array:
S(1).Name = 'ABC1';
S(1).Data = <3x8 doubles>
S(2).Name = 'AB56';
S(2).Data = <3x8 doubles>
and so on. Now S(k) contains the k's data set with its name.
Cells are an alternative:
NameList = {'ABC1', 'AB56', 'AC46', 'AC90'}
DataList = {3x8 doubles, 3x8 doubles, 6x8 doubles, 5x8 doubles}
Now the name NameList[k} corresponds to the data DataList{k}. As far as I understand, you have this data format already.
Trying to create a variable, which is called 'AC90' would be a very bad idea, see Answers: Don't EVAL!.
Another idea would using the names as fieldnames:
S.ABC1 = <3x8 doubles>
S.AB56 = <3x8 doubles>
...
Then:
key = 'AB56';
S.(key)

その他の回答 (1 件)

Sid Jhaveri
Sid Jhaveri 2017 年 4 月 10 日
編集済み: Sid Jhaveri 2017 年 4 月 10 日
For achieving this, you might want to use the " ismember " function of MATLAB.
You can also convert your cell array into a string array and use the " replace " or " strrep " functions.

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by