summing between array with different length
조회 수: 3 (최근 30일)
이전 댓글 표시
I have two arrays:
x = [ 1 2 3 4 5 6 7 8 9 0]; y = [ 6 7 8 9 ];
I'd like to add y in the middle of x so they form z
z = [1 2 3 10 12 14 16 8 9 0];
and second result (with shifting variable y) be
z = [1 2 3 4 11 13 15 17 8 0];
How would I go about doing this?
댓글 수: 0
답변 (1개)
BhaTTa
2024년 10월 21일
편집: BhaTTa
2024년 10월 21일
Hey @Moch Arief Albachrony, I assume that at first step you want to add elements of array y to elements of array x starting at index 4 till index 7 and in next step you want to shift the index by 1 and add them. Below I have provided the code to achieve it:
% Define the arrays
x = [1 2 3 4 5 6 7 8 9 0];
y = [6 7 8 9];
Idx = 4;
z1 = x; % Copy x to z1
z1(Idx:Idx+length(y)-1) = x(Idx:Idx+length(y)-1) + y;
% Second result: Insert y shifted by one position to the right
z2 = x; % Copy x to z2
z2(Idx+1:Idx+length(y)) = x(Idx+1:Idx+length(y)) + y;
% Display the results
disp('First result:');
disp(z1);
disp('Second result:');
disp(z2);
Hope it helps.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!