필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Ekram Adam
Ekram Adam 2017년 9월 4일
답변: Kris Fedorenko 2017년 9월 7일
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