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

4 vues (au cours des 30 derniers jours)
Ekram Adam
Ekram Adam le 4 Sep 2017
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)

Réponses (1)

Kris Fedorenko
Kris Fedorenko le 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

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by