create new variable in dataset from combinations of other variables

2 ビュー (過去 30 日間)
jonas
jonas 2013 年 3 月 14 日
Hi I have a problem regarding creating a new variable in a dataset array and assign it a value from information in the dataset Here is a simplified version of my dataset:
variables a b c observation [j k v; j r v; i r s; i k s; j k v; j k v]
I want to add a new variable and assign it a value depending on the combination of the variables a,b and c. So for example the combinations in a b c j k v = 1, j r v = 2,i r s = 3, i k s = 4 So for this short example I want to add a new variable to the dataset with the values 1,2,3,4,1,1
Final result should be: variables a b c d observation [j k v 1; j r v 2 ; i r s 3 ; i k s 4; j k v 1 ; j k v 1 ]
It's a large dataset but this illustrates what i want to do.
Help please?

採用された回答

Peter Perkins
Peter Perkins 2013 年 3 月 15 日
Create a vector of length 4 with the values you want, and assign
d.GroupIndex = values(i)
instead.
  2 件のコメント
jonas
jonas 2013 年 3 月 15 日
Great !! Thank you Peter
jonas
jonas 2013 年 3 月 15 日
Just noticed for my large dataset that the value vector has to be of equal length as the i vector...How can I code this? Say that the i vector has a length of 1000 and the values a want to assign is of 4 unique values as in my example..

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

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2013 年 3 月 14 日
Jonas, I think this is what you're looking for:
d = cell2dataset({'a' 'b' 'c'; 'j' 'k' 'v'; 'j' 'r' 'v'; 'i' 'r' 's'; 'i' 'k' 's'; 'j' 'k' 'v'; 'j' 'k' 'v'})
[~,~,i] = unique(d,[],'stable');
d.GroupIndex = i
  1 件のコメント
jonas
jonas 2013 年 3 月 15 日
Thank you Peter! That really helped me. Do you also have an answer how to assign a specific value to the unique rows in your code? For example I want the last column (i) to contain specific values, so the 1 in i should be 2.1, the 2= 6.5 the 3 = 4.2 and the 4 7.9..... BR Jonas

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

Community Treasure Hunt

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

Start Hunting!

Translated by