# How to keep an element fixed until it fits a condition?

3 views (last 30 days)
Sukru Yavuz on 8 Mar 2018
Commented: njj1 on 8 Mar 2018
for ii=1:4
deneme{ii,:}= vericell(class==ii,:);
for j = 4:14
deneme{ii}{:,j};
meanres(ii,j) = mean (deneme{ii}{:,j});
stdres(ii,j) = std(deneme{ii}{:,j});
uscalc(ii,j) = (predictioncolumn(j) - meanres(ii,j)) ^2 / (2* (stdres(ii,j)^2));
meancalc(ii,j) = 1/(sqrt(2*pi)* stdres(ii,j)) * (2.71^(uscalc(ii,j))) ;
bolumler{ii,:} = sum(veri.Class==ii)/ length(veri.Class);
likelihood(ii,j) = meancalc(ii,j) * bolumler{ii,:};
end
end
I want ii to stay as 1 while j keeps iterating until it reaches it's limit (14). And when it happens I want ii to be 2 and j to iterate from 4 to 14.

njj1 on 8 Mar 2018
Can you clean up the code a little? It's not clear where the loops begin and end... A couple more questions/comments.
1) How can j be greater than 14, since the loop only has j moving from 4 to 14?
2) Try not to use "length"; instead use size(x,dim) or numel(x).
3) If you want to accumulate the multiplication of meancalc(ii,j)*bolumler{ii,:}, then you could use the variable likelihood in the multiplication (i.e., likelihood(ii,j) = likelihood(ii,j-1)*meancalc(ii,j)).

Show 1 older comment
njj1 on 8 Mar 2018
Seems to me like your code should work as it stands. What exactly is going wrong?
Sukru Yavuz on 8 Mar 2018
I am really confused I am sorry. I need the multiplication of meancalc(ii,j) like: meancalc(1,4) * meancalc(1,5) * meancalc(1,6)... * meancalc(1,14) * bolumler{ii,:} I need that result.
njj1 on 8 Mar 2018
Right, so try this:
for ii=1:4
deneme{ii,:}= vericell(class==ii,:);
for j = 4:14
deneme{ii}{:,j};
meanres(ii,j) = mean (deneme{ii}{:,j});
stdres(ii,j) = std(deneme{ii}{:,j});
uscalc(ii,j) = (predictioncolumn(j) - meanres(ii,j)) ^2 / (2* (stdres(ii,j)^2));
meancalc(ii,j) = 1/(sqrt(2*pi)* stdres(ii,j)) * (2.71^(uscalc(ii,j))) ;
bolumler{ii,:} = sum(veri.Class==ii)/ length(veri.Class);
likelihood(ii,j) = meancalc(ii,j) * bolumler{ii,:};
end
end

Star Strider on 8 Mar 2018
The code you posted should do that.
The outer ‘ii’ loop begins at 1. The inner ‘j’ loop will iterate from 4 to 14 for each value of the outer loop ‘ii’ variable. Then ‘ii’ will increase by 1, the inner loop iterates, and the process continues until ‘ii’ is 4 and the final iteration of ‘j’ is 14.
Also, the code you posted is missing an end for the ‘ii’ loop. I assume you simply forgot to copy it.

Sukru Yavuz on 8 Mar 2018
Yes, sorry i forgot to copy that. I want ii to stay as 1 when j goes from 4 to 14. And when j finishes that, I want ii to increase to 2 and then j goes from 4 to 14 again.
Star Strider on 8 Mar 2018
Your code as you wrote it will do what you want.
This will allow you to see what your indices are doing:
for ii = 1:4
for j = 4:14
fprintf('ii = %2d\tj = %2d\n', ii,j)
end
end
fprintf('\n\n')
Sukru Yavuz on 8 Mar 2018
ii = 1 j = 4
ii = 1 j = 5
ii = 1 j = 6 ii = 1 j = 7
ii = 1 j = 8
ii = 1 j = 9
ii = 1 j = 10
ii = 1 j = 11
ii = 1 j = 12
ii = 1 j = 13
ii = 1 j = 14
The result is looking like this, thank you. Sorry about my confusion, my question is I want meancalc(1,4:14) to be multiplied like meancalc(ii,4) * meancalc(ii,5) ... meancalc(ii,14)