cast
변수를 다른 데이터형으로 형변환
설명
예제
int8형 값을 고정소수점으로 변환하기
8비트 정수 스칼라를 정의합니다.
a = int8(5);
워드 길이가 24
이고 소수부 길이가 12
인 부호 있는 fi
객체를 만듭니다.
p = fi([],1,24,12);
a
를 fi
객체 p
와 numerictype
, 실수/복소수 여부, fimath
가 동일해지도록 고정소수점으로 변환합니다.
b = cast(a, 'like', p)
b = 5 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 24 FractionLength: 12
배열을 고정소수점으로 변환하기
1로 구성된 2×3 행렬을 정의합니다.
A = ones(2,3);
워드 길이가 16
이고 소수부 길이가 8
인 부호 있는 fi
객체를 만듭니다.
p = fi([],1,16,8);
A
를 p
와 데이터형, 실수/복소수 여부가 동일해지도록 변환합니다.
B = cast(A,'like',p)
B = 1 1 1 1 1 1 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 8
데이터형에 종속되지 않는 MATLAB 코드 작성하기
알고리즘 자체를 변경하지 않고도 여러 다른 데이터형으로 실행할 수 있는 MATLAB® 알고리즘을 작성합니다. 알고리즘을 재사용하려면 알고리즘에서 데이터형 정의를 분리하십시오.
이 방법을 사용하면 부동소수점 데이터형으로 알고리즘을 실행하여 기준을 정의할 수 있습니다. 그런 다음 원래 MATLAB 코드를 수정하지 않고도 여러 다른 고정소수점 데이터형으로 알고리즘을 테스트하고 고정소수점 동작을 기준과 비교할 수 있습니다.
계수, 입력 데이터, 출력 데이터의 데이터형이 정의된 구조체 T
를 입력 파라미터로 받는 MATLAB 함수 my_filter
를 작성합니다.
function [y,z] = my_filter(b,a,x,z,T) % Cast the coefficients to the coefficient type b = cast(b,'like',T.coeffs); a = cast(a,'like',T.coeffs); % Create the output using zeros with the data type y = zeros(size(x),'like',T.data); for i = 1:length(x) y(i) = b(1)*x(i) + z(1); z(1) = b(2)*x(i) + z(2) - a(2) * y(i); z(2) = b(3)*x(i) - a(3) * y(i); end end
my_filter
를 부동소수점 계단 입력을 사용해서 호출하고 고정소수점 계단 입력을 사용해서 호출하는 MATLAB 함수 zeros_ones_cast_example
을 작성해서 결과를 비교합니다.
function zeros_ones_cast_example % Define coefficients for a filter with specification % [b,a] = butter(2,0.25) b = [0.097631072937818 0.195262145875635 0.097631072937818]; a = [1.000000000000000 -0.942809041582063 0.333333333333333]; % Define floating-point types T_float.coeffs = double([]); T_float.data = double([]); % Create a step input using ones with the % floating-point data type t = 0:20; x_float = ones(size(t),'like',T_float.data); % Initialize the states using zeros with the % floating-point data type z_float = zeros(1,2,'like',T_float.data); % Run the floating-point algorithm y_float = my_filter(b,a,x_float,z_float,T_float); % Define fixed-point types T_fixed.coeffs = fi([],true,8,6); T_fixed.data = fi([],true,8,6); % Create a step input using ones with the % fixed-point data type x_fixed = ones(size(t),'like',T_fixed.data); % Initialize the states using zeros with the % fixed-point data type z_fixed = zeros(1,2,'like',T_fixed.data); % Run the fixed-point algorithm y_fixed = my_filter(b,a,x_fixed,z_fixed,T_fixed); % Compare the results coder.extrinsic('clf','subplot','plot','legend') clf subplot(211) plot(t,y_float,'co-',t,y_fixed,'kx-') legend('Floating-point output','Fixed-point output') title('Step response') subplot(212) plot(t,y_float - double(y_fixed),'rs-') legend('Error') figure(gcf) end
입력 인수
a
— 다른 데이터형으로 형변환할 변수
fi
객체 | 숫자형 변수
변수로, fi
객체 또는 숫자형 변수로 지정됩니다.
복소수 지원: 예
p
— 프로토타입
fi
객체 | 숫자형 변수
프로토타입으로, fi
객체 또는 숫자형 변수로 지정됩니다. 복소수 객체를 지정할 때 프로토타입을 사용하려면 프로토타입 값을 지정해야 합니다. 그 외의 경우에는 값을 지정하지 않아도 됩니다.
복소수 지원: 예
팁
b = cast(a,'like',p)
구문을 사용하여 데이터형 지정을 알고리즘 코드와 분리하면 다음이 가능합니다.
여러 다른 데이터형에 대해 알고리즘 코드를 재사용할 수 있습니다.
데이터형 사양과 여러 데이터형에 대한 switch 문을 사용하면 알고리즘이 지저분해지지 않게 관리할 수 있습니다.
알고리즘 코드의 가독성을 높일 수 있습니다.
고정소수점 데이터형과 부동소수점 데이터형 간에 전환하여 기준을 비교할 수 있습니다.
알고리즘 코드를 변경하지 않고도 여러 변형의 고정소수점 설정 간에 전환할 수 있습니다.
버전 내역
R2013a에 개발됨
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)