how to have a loop if for a column in a table
1 回表示 (過去 30 日間)
古いコメントを表示
I have a table with many columns... I want to iterate into one column, that have many numbers valores. is there someone that can help me?
the pseudocode is
if table.column > number
table.new_column= 'Ok'
else
table.this_new column='nok'
finally i wil have a new columns with ok and nok depending of the valor of the column
0 件のコメント
採用された回答
Ameer Hamza
2018 年 5 月 14 日
編集済み: Ameer Hamza
2018 年 5 月 14 日
You don't need a for loop, use arrayfun(),
words = {'nok', 'ok'};
table.newColumn = arrayfun(@(x) words((x>number)+1), table.column);
3 件のコメント
Ameer Hamza
2018 年 5 月 14 日
It is not clear what you are trying to do but you can access and change values of a table just like a cell array using table{1,1} (first column, first element). Similarly, use can assign as
table{1,1} = 1; % it will assign the value
その他の回答 (1 件)
Peter Perkins
2018 年 5 月 14 日
Ameer's solution works, but arrayfun is really not needed:
words = {'nok', 'ok'};
table.newColumn = words(table.column>number)+1);
But also, a cell array of 'ok' and 'nok' is likely not all the helpful. I'd suggest making a categorical variable:
table.newColumn = categorical(table.column>number,[false,true],{'nok' 'ok});
Like Ameer, I'm not able to fully understand the followup question.
2 件のコメント
Peter Perkins
2018 年 5 月 17 日
I can't follow this. I suggest you open a new thread, with a short, clear example of what you have and what you want.
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!