An easy way for adding on pervious row that expands

조회 수: 1 (최근 30일)
Yaser Khojah
Yaser Khojah 2022년 3월 7일
댓글: Yaser Khojah 2022년 3월 8일
I have two questions that are brought from my codes where I need to perform to jobs as below. Is there an easy approach as I have large data.
Thanks for the help!
% First part
x1 = [100; 200; 50; 100];
Muliple = 0.1;
x1(1) .* Muliple = 10; % this will be sent to the following row
(x1(2) - 10) .* Muliple = 19; % this will be sent while adding the previous row (10 + 19)
(x1(3) - (10 + 19)) .* Muliple = 2.1; % (
(x1(4) - (10 + 19 + 2.1)) .* Muliple = 21.1;
% Second part
%Another Part I need help please how to divide x1 by 2 and send each half to the following row while adding
%for example
x2= [50; 50 + 100; 100 + 25; 25 + 50; 50];

채택된 답변

Davide Masiello
Davide Masiello 2022년 3월 7일
편집: Davide Masiello 2022년 3월 7일
x = [100; 200; 50; 100];
Muliple = 0.1;
x1 = zeros(size(x));
x1(1) = x(1)*Muliple;
for i = 2:length(x)
x1(i) = (x(i)- sum(x1(1:i-1)))*Muliple;
end
x2 = [x/2;0]+[0;x/2];
This yields
x1 =
10.0000
19.0000
2.1000
6.8900
x2 =
50
150
125
75
50

추가 답변 (1개)

Max Alger-Meyer
Max Alger-Meyer 2022년 3월 7일
First part (note that this follows the algorithm you described but the fourth that you listed for the first part is wrong):
x1 = [100; 200; 50; 100];
Multiple = 0.1;
x2 = zeros(size(x1));
for i = 1:numel(x2)
if i > 1
x2(i) = (x1(i)-sum(x2(1:(i-1))))*Multiple;
else
x2(i) = x1(i)*Multiple;
end
end
x2
x2 = 4×1
10.0000 19.0000 2.1000 6.8900
Second Part:
x2 = (x1)/2;
x3 = x2;
for i = 1:numel(x2)
if i > 1
x3(i) = x2(i) + x2(i-1);
end
end
x3(end+1) = x2(end);
x3
x3 = 5×1
50 150 125 75 50

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by