Save variable number in matrix for later iteration

15 views (last 30 days)
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

Torsten
Torsten on 19 Jan 2026
Edited: Torsten on 19 Jan 2026
Maybe like this ?
I'm not sure whether you want to take the elements of C and J according to the order of the elements in I or sort(I). In the case below, I and sort(I) are identical since I is strictly increasing.
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
I = zeros(1,lim);
for i = 1:lim
I(i) = (V(i)^2-1)/3;
end
I
I = 1×4
8 16 40 56
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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)
J = 1×4
10 14 22 26
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C = J-I
C = 1×4
2 -2 -18 -30
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Val = (1:max(I))/lim;
count = 0;
for k = I
count = count + 1;
for j = k:max(I)
Val(j) = Val(j) - (j + C(count))/J(count);
end
end
Val(8)
ans = 1
8/4 - ((8+2)/10)
ans = 1
Val(9)
ans = 1.1500
9/4 - ((9+2)/10)
ans = 1.1500
Val(18)
ans = 1.3571
18/4 - ((18+2)/10 + (18-2)/14)
ans = 1.3571
  1 Comment
Joan
Joan on 19 Jan 2026
This is exactly what I wanted, thank you so much!!! It only had to be changed the first definition of "Val" as it has to be divided by 4 and not by "lim", but the rest runs flawlessly. Again, thank you.

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 18 Jan 2026
Edited: Stephen23 on 18 Jan 2026
"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)
V = 1×4
5 7 11 13
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Vectorized I calculation
I = (V.^2 - 1) / 3;
I = round(I)
I = 1×4
8 16 40 56
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% 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)
J1 = 1×4
3 5 7 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
increments_J2 = 2*ones(1,lim-1);
increments_J2(2:2:end) = 6; % [2, 6, 2, 6, ...]
J2(2:lim) = 7 + cumsum(increments_J2)
J2 = 1×4
7 9 15 17
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
J = J1 + J2
J = 1×4
10 14 22 26
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C = J - I
C = 1×4
2 -2 -18 -30
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% 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
K = 7.8298
Val
Val = 1×56
0.2500 0.5000 0.7500 1.0000 1.2500 1.5000 1.7500 1.4000 1.6500 1.9000 2.1500 2.4000 2.6500 2.9000 3.1500 2.1143 2.3643 2.6143 2.8643 3.1143 3.3643 3.6143 3.8643 4.1143 4.3643 4.6143 4.8643 5.1143 5.3643 5.6143
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  3 Comments
Joan
Joan on 18 Jan 2026
Edited: Joan on 18 Jan 2026
First of all, thanks a LOT for this response. I'm trying to learn to vectorize and this helps a ton.
Having said this, my main problem still persists as I want all values marked as "i" in "K" to change with every iteration. For exemple, for i=8 I want
Val(8) = 8/4 - ((8+2)/10) = 1
for i=9
Val(9) = 9/4 - ((9+2)/10) = 23/20
for i=18
Val(18) = 18/4 - ((18+2)/10 + (18-2)/14) = 19/14
and so.
The code as it is now assingns one "i" value to each set of "K" and I want it to change for each iteration.
PD: I have corrected the formula that I want. Before I posted K=(i-C)/J and I wanted to say K=(i+C)/J, sorry for the confusion that this may cause.
Stephen23
Stephen23 on 19 Jan 2026
Edited: Stephen23 on 19 Jan 2026
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)
V = 1×4
5 7 11 13
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Vectorized I calculation
I = round((V.^2 - 1) / 3)
I = 1×4
8 16 40 56
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% 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)
J1 = 1×4
3 5 7 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
increments_J2 = 2*ones(1,lim-1);
increments_J2(2:2:end) = 6; % [2, 6, 2, 6, ...]
J2(2:lim) = 7 + cumsum(increments_J2)
J2 = 1×4
7 9 15 17
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
J = J1 + J2
J = 1×4
10 14 22 26
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C = J - I
C = 1×4
2 -2 -18 -30
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% 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)
ans = 1
8/4 - ((8+2)/10)
ans = 1
Val(9)
ans = 1.1500
9/4 - ((9+2)/10)
ans = 1.1500
23/20
ans = 1.1500
Val(18)
ans = 1.3571
18/4 - ((18+2)/10 + (18-2)/14)
ans = 1.3571
19/14
ans = 1.3571

Sign in to comment.

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!