필터 지우기
필터 지우기

how to have a loop if for a column in a table

조회 수: 2 (최근 30일)
Jessica Blasco
Jessica Blasco 2018년 5월 14일
댓글: Peter Perkins 2018년 5월 17일
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

채택된 답변

Ameer Hamza
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
Jessica Blasco
Jessica Blasco 2018년 5월 14일
ok, this solution works, but i have another question ... in my case, i have 8 types de variables in a column (1..8) for each type i have a value limit,(stored them in a vector) So... limit_value=[ number1, number 2, number 3,....,number 8] i want to evaluate for each type, the value that corresponds in the limit value.
type 1 -> evaluate > number 1
type 2-> evaluate > number 2
....
but i want to have with a loop, it's posible?
thanks!
Ameer Hamza
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
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
Jessica Blasco
Jessica Blasco 2018년 5월 15일
i have a vector with 8 numbers,so v=[number1, number2,...., number8] In my column i have 8 categories of number, 1..8...
i would like iterate the column such that for each number in the column i can evaluate if this valor is mayor a his position in the vector...
so...
column
1 evaluate number in position 1 of the vector
1 evaluate number in position 1 of the vector 2 evaluate number position 2 of the vector
3 evaluate number in the position 3 of the vector
4 evaluate number in the position 4 of the vector
2 evaluate number in the position 2 of the vector
1 evaluate number in the position 1 of the vector
5 evaluate number in the position 5 of the vector
I would like iterate this table with a loop for, it's posible??
Peter Perkins
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 CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by