# Hi all, I want to write a code that assign new column to the table. For example, I have a column like:

2 vues (au cours des 30 derniers jours)
Gokhan Kayan le 17 Jan 2018
Land Cover Soil Group Value
Forest A
Urban B
Vegetation C
I want to create new column called "Value". This "Value" depends on Land Cover and Soil Group. For example If Land Cover is Forest and Soil Group is B, Value should be 50. Or If Land Cover is Urban and Soil Group is C, Value should be 75. How can I write it ? Thank you for your help
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponses (1)

Peter Perkins le 17 Jan 2018
Part of this question has nothing to do with tables, the other part does:
1) Not sure how 50 comes from Forest,B, but let's assume you have a desired value for every possibility in the 3x3 set of combinations. You can make a matrix, and use sub2ind to get the right elements for each actual combination.
2) If your table is called t, and your function is called fun, a simple assignment using dot subscripting will create a new variable.
Consider using a table, and categorical variables for LandCover and SoilType. Put all that together, and it looks like this:
>> LandCover = categorical([1;2;3;1;2;3],1:3,{'Forest' 'Urban' 'Vegetation'});
>> SoilType = categorical([1;1;2;2;3;3],1:3,{'A' 'B' 'C'});
>> t = table(LandCover,SoilType)
t =
6×2 table
LandCover SoilType
__________ ________
Forest A
Urban A
Vegetation B
Forest B
Urban C
Vegetation C
>> t.Value = fun(t.LandCover,t.SoilType)
t =
6×3 table
LandCover SoilType Value
__________ ________ _____
Forest A 1
Urban A 4
Vegetation B 8
Forest B 50
Urban C 75
Vegetation C 9
with
function v = fun(lc,st)
values = [1 50 3; 4 5 75; 7 8 9];
i = sub2ind([3 3],double(lc),double(st));
v = values(i);
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Control System Toolbox dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by