JChat
JChat

Reputation: 814

How to add values to last column of a table based on certain conditions in MATLAB?

I have a 29736 x 6 table, which is referred to as table_fault_test_data. It has 6 columns, with names wind_direction, wind_speed, air_temperature, air_pressure, density_hubheight and Fault_Condition respectively. What I want to do is to label the data in the Fault_Condition (last table column with either a 1 or a 0 value, depending on the values in the other columns.

I would like to do the following checks (For eg.)

  1. If wind_direction value(column_1) is below 0.0040 and above 359.9940, label 6 th column entry corresponding to the respective row of the table as a 1, else label as 0.
  2. Do this for the entire table. Similarly, do this check for others like air_temperature, air_pressure and so on. I know that if-else will be used for these checks. But, I am really confused as to how I can do this for the whole table and add the corresponding value to the 6 th column (Maybe using a loop or something).

Any help in this regard would be highly appreciated. Many Thanks!

EDIT: Further clarification: I have a 29736 x 6 table named table_fault_test_data . I want to add values to the 6 th column of table based on conditions as below:-

for i = 1:29736 % Iterating over the whole table row by row
    if(1st column value <x  | 1st column value > y)
         % Add 0 to the Corresponding element of 6 th column i.e. table_fault_test_data(i,6)
    elseif (2nd column value <x  | 2nd column value > y)
         % Add 0 to the Corresponding element of 6 th column i.e. table_fault_test_data(i,6)
    elseif ... do this for other cases as well
    else
         % Add 1 to the Corresponding element of 6 th column i.e. table_fault_test_data(i,6)

This is the essence of my requirements. I hope this helps in understanding the question better.

Upvotes: 0

Views: 131

Answers (1)

pacta_sunt_servanda
pacta_sunt_servanda

Reputation: 734

You can use logical indexing, which is supported also for tables (for loops should be avoided, if possible). For example, suppose you want to implement the first condition, and also suppose your x and y are known; also, let us assume your table is called t

logicalIndecesFirstCondition = t{:,1} < x | t{:,2} >y

and then you could refer to the rows which verify this condition using logical indexing (please refer to logical indexing

E.g.:

t{logicalIndecesFirstCondition  , 6} = t{logicalIndecesFirstCondition  , 6} + 1.0; 

This would add 1.0 to the 6th column, for the rows for which the logical condition is true

Upvotes: 2

Related Questions