An easy way for adding on pervious row that expands
Show older comments
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];
Accepted Answer
More Answers (1)
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
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
1 Comment
Yaser Khojah
on 8 Mar 2022
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!