how to Add two arrays representing integers base B--Test+function?

1 次查看(过去 30 天)
Pseudo-Code: The variables are X, Y, Z, C which are all flipped arrays of proper size. This means that x₀ should be denoted X(1). 1.Add the two arrays X and Y and call it XpY element by element 2.for element i if XpY(i)> B-1, then XpY(i)=XpY(i)-B. Else do nothing. 3.If C=0 (every element is zero) then Z=XpY and I am done. Else X=XpY and Y=C, and repeat. X = [0, 6,6,6,6] ; Y = [0, 6,6,6,1]; % represents 6661 in base 7 B=7 ; [Z] = AddXYBaseB(X, Y, B) AddXYBaseB([6,6],[6,6],7)

回答(1 个)

Kris Fedorenko
Kris Fedorenko 2017-9-7
Hi Ekram!
Element-wise addition of two vectors (of the same length) can be achieved simply by using the " + " operator in MATLAB. For example:
>> A = [ 1, 2, 3];
>> B = [ 1, 0, 5];
>> C = A+B
C =
2 2 8
For editing the XpY vector based on its values, consider examples in the following link: https://www.mathworks.com/help/matlab/matlab_prog/find-array-elements-that-meet-a-condition.html#bt_xjh4
For checking whether the vector contains all zeros, you can look into the " nnz " function.
You might also find MATLAB's conditional statements (if statements) and loops useful.
Hope this helps!
Kris

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by