Help with for loop.

12 views (last 30 days)
ajk1
ajk1 on 19 Apr 2015
Commented: dpb on 21 Apr 2015
Hi, I have a for loop where d is a 100x1 array and d0 is the average value of the elements in d. I would like to know how I can change it so after each iteration I have a 100x1 result in final_table and after all iterations are completed the variable final_table is a 100x100 array. The way I have implemented the if loop is incorrect since I finish with a 50x100 array. What I am trying to implement is to go through the different values of d, if less than the average d0 then calculate final (variables final_N1 and final_No) to hopefully finish with a 100x1 result and repeat 100 times to finish with 100x100 array in final_table. Thanks.
for cnt=1:100
if d(cnt)<=d0
No=sum(d<=d0);
d_pos_No=find(d<=d0);
d_pos_No=d(d_pos_No);
final_No=d_pos_No/No;
final_N1=zeros(size(final_No));
else
N1=sum(d>d0);
d_pos_N1=find(d>d0);
d_pos_N1=d(d_pos_N1);
final_N1=d_pos_N1/No;
final_No=zeros(size(final_N1));
end
final_table(1:numel(final_No+final_N1),cnt)=final_No+final_N1;
end
  15 Comments
dpb
dpb on 21 Apr 2015
I repeat for the last time or I'm outta' here...give a complete set of inputs and outputs desired...your initial post says a length(100) vector should produce a 100x100 output; I presume that means the sample 5-vector should be 5x5 but I can't decipher from the descriptions what the algorithm is supposed to be. You're much like a fella' I once did contract work for for DOE who I told on numerous occasions that he like to manage by asking for a rock. When asked "What kind of rock?" his reply would be something like "I'll know it when you finally bring it to me." I tired of that game some 30 years ago when I was much younger and needed the work...
ajk1
ajk1 on 21 Apr 2015
Edited: ajk1 on 21 Apr 2015
The input is d which is generated randomly ealier in the code where (sorry for not including this earlier), d = rand(5,1); Where in this example case cnt=1:5.
First iteration (cnt=1) d= [0.2433, 0.2226, 0.2486, 0.2861, 0.0673]; mean(d)= 0.2136 res= 0.2432 0.2225 0.2485 0.2859 1.0000
Second iteration (cnt=2) d= [0.3247, 0.1254, 0.1608, 0.0724, 0.5560]; mean(d)= 0.24786 res= 0.3687 0.3497 0.4484 0.2019 0.6313
Third iteration (cnt=3) d= [0.4749, 0.0868, 0.0620, 0.4488, 0.7260]; mean(d)= 0.3597 res= 0.2879 0.5833 0.4167 0.2720 0.4401
Fourth iteration (cnt=4) d= [0.1534, 0.5303, 0.0225, 0.1750, 0.6728]; mean(d)= 0.3108 res= 0.4372 0.4408 0.0641 0.4987 0.5592
Fifth iteration (cnt=5) d= [0.2347, 0.2541, 0.6018, 0.4240, 0.6654]; mean(d)= 0.5450 res= 0.2571 0.2784 0.4749 0.4645 0.5251
Output:
final_table=
0.2432 0.3687 0.2879 0.4372 0.2571
0.2225 0.3497 0.5833 0.4408 0.2784
0.2485 0.4484 0.4167 0.0641 0.4749
0.2859 0.2019 0.2720 0.4987 0.4645
1.0000 0.6313 0.4401 0.5592 0.5251

Sign in to comment.

Accepted Answer

dpb
dpb on 21 Apr 2015
OK, that's enough to figure out that the iteration isn't somehow dependent upon what did the first but the data are independent. There's no need for loops over the vectors; use Matlab's logical addressing features--for each column the calculation is simply
idx=d(:,icol)<=mean(d(:,icol)); % the logical index for the column
res(idx,icol)=d(idx,icol)/sum(d(idx,icol)); % the locations <= are true
idx=~idx; % the > locations are the logical negation
res(idx,icol)=d(idx,icol)/sum(d(idx,icol)); % set those
The above is in a loop over
icol=1:length(d)
the number of columns in the original array. The total of idx and ~idx includes every row so the length is the same. It will make a little speedup overall if you were to preallocate the res array as
res=zeros(length(d));
before beginning the loop if the array is quite large; otherwise it will be "growed" in size adding a column at a time.
Unless your other code is dependent upon only building a single element at a time for the d vector/array, you really don't need explicit loops at all. If it is fully a case of generating a random table, then
N=5; % the end size desired
d=rand(5); % the array or values (random or otherwise generated from the previous code)
and you can write the above logic and use accumarray to build the final result.
  2 Comments
ajk1
ajk1 on 21 Apr 2015
Thanks dpb, this will simplify my code.
dpb
dpb on 21 Apr 2015
No problem; it just shows, however, that we don't know what you do about your requirements but only what is posted. Writing clearly and completely defining expectations is the key to getting useful responses in the fewest number of iterations.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!