条件に沿って数値を変換する
    2 views (last 30 days)
  
       Show older comments
    
スプレットシートで数値の配列があります.
これを下記のようにグループ分けルールに沿って変換し,
スプレットシートやテキストで保存したいです.
■グループ分けルール
入力	     → 変換後の出力
1     	→ 1
2~5  のいずれかの場合      → 2
6,8,10 のいずれかの場合    → 3
7,9,11 のいずれかの場合  → 4
■変換
入力    →	変換後の出力
1          → 1
2          → 2
5          → 2
9          → 4
0 Comments
Accepted Answer
  Atsushi Ueno
      
 on 25 Feb 2024
        writematrix(-3:15,'matrix.xls'); % スプレットシートで数値の配列
a = readmatrix('matrix.xls')
b = arrayfun(@f, a)
writematrix(b,'output.txt'); % writematrix(b,'output.xls'); % スプレットシートやテキストで保存
type output.txt
function out = f(in) % グループ分けルールに沿って変換
    in = floor(in); % 暫定仕様:小数は切り捨てる
    if in < 1
        out = NaN; % 暫定仕様:範囲外はNaNを返す
    elseif in < 2
        out = in; % 1→1
    elseif in < 6
        out = 2; % 2~5のいずれかの場合→2
    elseif in < 12
        out = mod(in,2) + 3; % 6,8,10のいずれかの場合→3、7,9,11のいずれかの場合→4
    else
        out = NaN; % 暫定仕様:範囲外はNaNを返す
    end
end
More Answers (1)
  Dyuman Joshi
      
      
 on 25 Feb 2024
        %Random data for example
in = randi(11, 1, 10)
out = discretize(in, 0:11, [1 2 2 2 2 3 4 3 4 3 4], 'IncludedEdge', 'right')
2 Comments
See Also
Categories
				Find more on スプレッドシート in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!