------------------------------------------------------------------------------------------------
Title : Converting a fractional sequence in given base to another base
Author : Amir R. Forouzan
Email : a.forouzan@eng.ui.ac.ir
Company : University of Isfahan
Created on : 3 Feb. 2025
------------------------------------------------------------------------------------------------
Function : baseNtobaseM
Purpose : Converts a fractional number in base N to a number in base M up to K fractional points.
Inputs :
- seqX : Vector representing the input number in base N. Each element seqX(k)
represents the k-th fractional point of X and is a non-negative integer < N.
- N : The base of the input number seqX.
- M : The base to which the input number seqX is to be converted.
- K : Number of fractional points to which the output number seqY is to be computed.
Outputs :
- seqY : Vector representing the output number in base M. Each element seqY(k)
represents the k-th fractional point of Y and is a non-negative integer < M.
- remX : Vector representing the remainder in base N. Each element remX(k)
represents the k-th fractional point of R and is a non-negative integer < N.
Description:
seqX represents X = (0.seqX(1) seqX(2) seqX(3) ...) in base N.
seqY represents Y = (0.seqY(1) seqY(2) seqY(3) ...) in base M.
remX represents R = (0.remX(1) remX(2) remX(3) ...) in base N.
The function converts seqX to seqY and calculates remX such that X = Y + R / N^K.
Example :
Converting X = 0.5 in base 2 to base 3 up to 4 fractional points.
X in base 2 is represented by seqX = [1] and N = 2, M = 3, K = 4.
[seqY, remX] = baseNtobaseM([1], 2, 3, 4)
Results:
seqY =
1
1
1
1
remX =
1
Thus Y = 1/3 + 1/9 + 1/27 + 1/81 and R = 1/2.
We see that Y + R / 81 = 0.5 as expected.
Code:
Y = 1/3 + 1/9 + 1/27 + 1/81;
R = 1/2;
result = Y + R / 81
result =
0.5000
引用格式
Amir R. Forouzan (2025). Fractional base M to N conversion (https://www.mathworks.com/matlabcentral/fileexchange/<...>), MATLAB Central File Exchange. Retrieved February 3, 2025.
MATLAB 版本兼容性
创建方式
R2024b
兼容任何版本
平台兼容性
Windows macOS Linux标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!