Clear Filters
Clear Filters

Indexing in nested loops x 3

3 views (last 30 days)
Jade T
Jade T on 21 Jul 2023
Commented: Jade T on 25 Jul 2023
My title may not make sense, but I do not know the technical term. In the code below, I want to use the elements in sv_10 to index into pairs, which can only be done by indexing via sv_gain/sv_loss. However, I have tried several ways to do this and none of them work. Please help
G = 75:5:110;
L = 15:5:50;
[g,l] = meshgrid(G,L); %returns 2-D grid; The grid represented by the coordinates g and l has length(L) rows and length(G) columns
pairs = [g(:) l(:)];
p = 0.5 % 0.1:0.1:1 % one p
delta = 0.6;
gamma = .5; % (0.2:0.1:0.8);
y = zeros(length(p),length(gamma));
sv_gain = zeros(length(pairs),length(p))
sv_loss = zeros(length(pairs),length(p))
sv_gamble = zeros(length(pairs),length(p))
for i = 1:length(p)
y(i) = (delta .* p(i).^gamma/(delta .* p(i).^gamma + (1 - p(i)).^gamma))
for c = 1:length(pairs)
sv_gain(c,i) = pairs(c,1).* y(i) % sv_gain
sv_loss(c,i) = pairs(c,2).* (y(i).* 2) %sv_loss
sv_gamble(c,i) = (sv_gain(c))- (sv_loss(c))
ev(c) = (pairs(c,1)* p(i)) + (pairs(c,2)* p(i))
end
end
ev = transpose(round(ev))
sv_gamble = round(sv_gamble)
upbound = 10
lowbound = -10
sv_10 = sv_gamble(sv_gamble >= lowbound & sv_gamble <= upbound)
g_10 = [];
l_10 = [];
pairs_10 = [];
for k = 1:length(sv_10)
idx = find(round((sv_gain-sv_loss) == sv_10(k)))
for o= 1:length(idx)
idx_gain = idx(o)
idx_loss = find(sv_loss == sv_gain(idx_gain)-sv_10(k))
if ~isempty(idx_loss)
pairs_10 = [pairs_10; idx_gain, idx_loss];
end
end
end
This returns empty vectors which should contain the values that correspond to sv_gain/sv_loss, and then after I would be able to use those to index into pairs

Accepted Answer

Matt J
Matt J on 22 Jul 2023
Edited: Matt J on 22 Jul 2023
You are using '==' in several places in your code. When you do so, you must remember the limitations of floating point math,
0.7*0.4==0.28
ans = logical
0
abs(0.7*0.4-0.28)
ans = 5.5511e-17

More Answers (0)

Categories

Find more on Multidimensional Arrays 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!