条件に沿って数値を変換する

スプレットシートで数値の配列があります.
これを下記のようにグループ分けルールに沿って変換し,
スプレットシートやテキストで保存したいです.
■グループ分けルール
入力   → 変換後の出力
1   → 1
2~5  のいずれかの場合 → 2
6,8,10 のいずれかの場合 → 3
7,9,11 のいずれかの場合  → 4
■変換
入力 → 変換後の出力
1 → 1
2 → 2
5 → 2
9 → 4

 Respuesta aceptada

Atsushi Ueno
Atsushi Ueno el 25 de Feb. de 2024

0 votos

writematrix(-3:15,'matrix.xls'); % スプレットシートで数値の配列
a = readmatrix('matrix.xls')
a = 1x19
-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
b = arrayfun(@f, a)
b = 1x19
NaN NaN NaN NaN 1 2 2 2 2 3 4 3 4 3 4 NaN NaN NaN NaN
writematrix(b,'output.txt'); % writematrix(b,'output.xls'); % スプレットシートやテキストで保存
type output.txt
NaN,NaN,NaN,NaN,1,2,2,2,2,3,4,3,4,3,4,NaN,NaN,NaN,NaN
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

1 comentario

H.O
H.O el 26 de Feb. de 2024
ありがとうございます.実装できました.

Iniciar sesión para comentar.

Más respuestas (1)

Dyuman Joshi
Dyuman Joshi el 25 de Feb. de 2024

2 votos

%Random data for example
in = randi(11, 1, 10)
in = 1×10
10 5 7 10 3 5 5 11 10 6
out = discretize(in, 0:11, [1 2 2 2 2 3 4 3 4 3 4], 'IncludedEdge', 'right')
out = 1×10
3 2 4 3 2 2 2 4 3 3

2 comentarios

H.O
H.O el 26 de Feb. de 2024
Thank you for your answer
Atsushi Ueno
Atsushi Ueno el 26 de Feb. de 2024
I like it! This is exactly the MATLAB skills!

Iniciar sesión para comentar.

Productos

Versión

R2021b

Etiquetas

Preguntada:

H.O
el 24 de Feb. de 2024

Comentada:

el 26 de Feb. de 2024

Community Treasure Hunt

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

Start Hunting!