필터 지우기
필터 지우기

How to do this addition?

조회 수: 3 (최근 30일)
Aswin Sandirakumaran
Aswin Sandirakumaran 2018년 5월 27일
답변: Image Analyst 2018년 5월 27일
D = [60,600,15];
A(2) = D(2); -> OUTPUT OF A LOOKS LIKE A = [0,600]
B(3) = D(3); -> OUTPUT OF B LOOKS LIKE B = [0,0,15]
c = A + B;
disp(c);
How to make length of A as 3 and fill in zeros where the index is not used

채택된 답변

Image Analyst
Image Analyst 2018년 5월 27일
If you want A to be the same size as B, which may be less than the length of D depending on which indexes you're setting, then use Ameer's code, as long as B is also longer than A.
If you want A, B, and c all to be the same length as D, then do this:
% Initialize variables.
D = [60,600,15];
A = zeros(size(D)); % All zeros and the same size as D.
B = zeros(size(D));
% Now set whatever elements of A and B you want
% For example...
A(2) = D(2); % OUTPUT OF A LOOKS LIKE A = [0, 600, 0]
B(3) = D(3); % OUTPUT OF B LOOKS LIKE B = [0, 0, 15]
% Now add.
c = A + B;

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2018년 5월 27일
Make A same size as B
A = [A zeros(1, length(B)-length(A))]
and then add
C = A+B;

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by