Changing values in atable
    6 ビュー (過去 30 日間)
  
       古いコメントを表示
    
The data i want to change is in the 4th column.
I have tried using an IF statment but does not work:
for 
       if 
    table{:,4} >=5
   disp '1'
    else 
        disp '0'
    end
Thank you in advance.
0 件のコメント
回答 (3 件)
  Stephan
      
      
 2021 年 3 月 20 日
        % Example table with random data
T = splitvars(table(randi(10,10,5)))
T =
  10×5 table
    Var1_1    Var1_2    Var1_3    Var1_4    Var1_5
    ______    ______    ______    ______    ______
       9         7         2        10        7   
       3         5         2         2        8   
       7         7         1         2        6   
       6         6         5         7        4   
       6         7         5         1        2   
       9         6         4         6        6   
       3         8         8         6        3   
       4         6         7         9        1   
       2        10         8         5        8   
      10         3        10         4        3 
% Some magic happens by logical indexing:
T{:,4} = T{:,4} >= 5
T =
  10×5 table
    Var1_1    Var1_2    Var1_3    Var1_4    Var1_5
    ______    ______    ______    ______    ______
       9         7         2        1         7   
       3         5         2        0         8   
       7         7         1        0         6   
       6         6         5        1         4   
       6         7         5        0         2   
       9         6         4        1         6   
       3         8         8        1         3   
       4         6         7        1         1   
       2        10         8        1         8   
      10         3        10        0         3   
0 件のコメント
  Star Strider
      
      
 2021 年 3 月 20 日
        Try something like this: 
T1 = array2table(randi(9, 10, 3))
Lv = T1.Var2 > 5;
T1.Var2(Lv,:) = 1;
T1.Var2(~Lv,:) = 0
producing: 
T1 =
  10×3 table
    Var1    Var2    Var3
    ____    ____    ____
     4       6       5  
     3       1       3  
     2       1       1  
     2       3       6  
     4       5       8  
     1       6       4  
     6       4       1  
     5       8       3  
     7       7       2  
     7       9       3  
T1 =
  10×3 table
    Var1    Var2    Var3
    ____    ____    ____
     4       1       5  
     3       0       3  
     2       0       1  
     2       0       6  
     4       0       8  
     1       1       4  
     6       0       1  
     5       1       3  
     7       1       2  
     7       1       3
.
4 件のコメント
  Stephan
      
      
 2021 年 3 月 20 日
				Or if you want to work on an array replace the curly braces by normal braces
  Sergio Yanez-Pagans
      
 2021 年 3 月 20 日
        
      編集済み: Sergio Yanez-Pagans
      
 2021 年 3 月 20 日
  
      A good way to get what you want is to convert your table to an array. Here is an example:
column1 = [2,3,4,5]'; % Data for table columns
T = table(column1); % Creating example column table
A = table2array(T) % Convert yout table to an aray for the moment
A > 5 % Will return array with 0s & 1s that satisfy the condition
Hope you find this useful!
0 件のコメント
参考
カテゴリ
				Help Center および File Exchange で Data Type Conversion についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!