Save variable number in matrix for later iteration
Show older comments
Ok, this is the code I have:
clc, clearvars, home,
lim = input("Limit value: ");
V = zeros(1,lim);
V(1) = 5;
for i = 2:lim
if mod(i,2) == 0
V(i) = V(i-1) + 2;
else
V(i) = V(i-1) + 4;
end
end
I = zeros(1,lim);
for i = 1:lim
I(i) = (V(i)^2-1)/3;
end
J1 = zeros(1,lim);
J2 = zeros(1,lim);
J1(1) = 3;
J2(1) = 7;
for i = 2:lim
if mod(i,2) == 0
J1(i) = J1(i-1) + 2;
J2(i) = J2(i-1) + 2;
else
J1(i) = J1(i-1) + 2;
J2(i) = J2(i-1) + 6;
end
end
J12 = [J1; J2];
J = sum(J12,1);
C = J-I;
K(1) = @(x) (x+C(1))/J(1);
for i = 2:lim
K(i) = @(x) K{i-1} + (x+C(i))/J(i);
end
Val = zeros(1,max(I));
K = 0;
count = 0;
for i = 1:max(I)
if ismember(i,I) == 1
count = count + 1;
K = K + K1(count)*i + K2(count);
Val(i) = i/4 - (K);
else
Val(i) = i/4 - (K);
end
end
It basicaly calculates three matrixes with values I, J and C with length "lim" and now I want to make a for loop where the value is for each i
Val(i) = i/4 - K
where I want K to be
K = i-C/J % count times
The thing is I want K to grow each time i appears in the matrix I, so for 2 occurences its desired value is
K = (i-C(1))/J(2) + (i-C(2))/J(2)
for 4 occurences is
K = (i-C(1))/J(2) + (i-C(2))/J(2) + (i-C(3))/J(3) + (i-C(4))/J(4)
and so.
The problem I'm facing is how to call a variable that contains a value that can change with the current index in the loop. I tried with functions, with the symbolic toolbox... And I'm currently stucked as all I make contains some kind of error.
If it's not clear what I want to make respond so I will clear it out. Thanks in advance for the help.
Accepted Answer
More Answers (1)
"The problem I'm facing is how to call a variable that contains a value that can change with the current index in the loop. I tried with functions, with the symbolic toolbox... "
All you are doing is adding numbers, so you do not need complicated things like function handles or the symbolic toolbox. Here is a much simpler approach using SUM:
lim = 4;
% Vectorized V calculation using alternating pattern
increments = 2*ones(1,lim-1);
increments(2:2:end) = 4; % [2, 4, 2, 4, ...]
V = 5*ones(1,lim-1);
V(2:lim) = 5+cumsum(increments)
% Vectorized I calculation
I = (V.^2 - 1) / 3;
I = round(I)
% Vectorized J1 and J2 calculation
J1 = zeros(1,lim);
J2 = zeros(1,lim);
J1(1) = 3;
J2(1) = 7;
increments_J1 = 2*ones(1,lim-1);
J1(2:lim) = 3 + cumsum(increments_J1)
increments_J2 = 2*ones(1,lim-1);
increments_J2(2:2:end) = 6; % [2, 6, 2, 6, ...]
J2(2:lim) = 7 + cumsum(increments_J2)
J = J1 + J2
C = J - I
% Vectorized Val calculation with cumulative K
Val = zeros(1,max(I));
K = 0;
for i = 1:max(I)
% Find all indices where I equals the current i
indices = find(I==i);
% Vectorized accumulation for all matching indices
if ~isempty(indices)
K = K + sum((i - C(indices)) ./ J(indices));
end
Val(i) = i/4 - K;
end
K
Val
3 Comments
Compared to your original (non-vectorized) code:
lim = 4;
V = zeros(1,lim);
V(1) = 5;
for i = 2:lim
if mod(i,2) == 0
V(i) = V(i-1) + 2;
else
V(i) = V(i-1) + 4;
end
end
V
I = zeros(1,lim);
for i = 1:lim
I(i) = (V(i)^2-1)/3;
end
I
J1 = zeros(1,lim);
J2 = zeros(1,lim);
J1(1) = 3;
J2(1) = 7;
for i = 2:lim
if mod(i,2) == 0
J1(i) = J1(i-1) + 2;
J2(i) = J2(i-1) + 2;
else
J1(i) = J1(i-1) + 2;
J2(i) = J2(i-1) + 6;
end
end
J1
J2
J12 = [J1; J2];
J = sum(J12,1)
C = J-I
lim = 4;
% Vectorized V calculation using alternating pattern
increments = 2*ones(1,lim-1);
increments(2:2:end) = 4; % [2, 4, 2, 4, ...]
V = 5*ones(1,lim);
V(2:lim) = 5 + cumsum(increments)
% Vectorized I calculation
I = round((V.^2 - 1) / 3)
% Vectorized J1 and J2 calculation
J1 = zeros(1,lim);
J2 = zeros(1,lim);
J1(1) = 3;
J2(1) = 7;
increments_J1 = 2*ones(1,lim-1); % [2, 2, 2, ...]
J1(2:lim) = 3 + cumsum(increments_J1)
increments_J2 = 2*ones(1,lim-1);
increments_J2(2:2:end) = 6; % [2, 6, 2, 6, ...]
J2(2:lim) = 7 + cumsum(increments_J2)
J = J1 + J2
C = J - I
% Initialize Val
Val = (1:max(I)) / 4;
% Apply corrections for each element in I
for count = 1:lim
k = I(count);
% Subtract (j + C(count))/J(count) from all Val(j) where j >= k
Val(k:end) = Val(k:end) - ((k:max(I)) + C(count)) ./ J(count);
end
% Display results
Val(8)
8/4 - ((8+2)/10)
Val(9)
9/4 - ((9+2)/10)
23/20
Val(18)
18/4 - ((18+2)/10 + (18-2)/14)
19/14
Categories
Find more on Startup and Shutdown 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!