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

4 views (last 30 days)
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)

Answers (1)

Kris Fedorenko
Kris Fedorenko on 7 Sep 2017
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

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!