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

Ajk1 - please format your code so that it is more readable. You seem to be missing an end for the for loop. Also, include some sample values for d and No so we can get an idea as to what is happening. You may also want to review MATLAB debugging so that you can step through the code yourself to see what is happening.
dpb
dpb on 19 Apr 2015
I'm pretty sure you don't need any loop at all but I can't decipher the end result desired from the description, sorry.
How about forgetting about the code and describe the algorithm and demonstrate for a tiny (say 5 elements, max) example?
ajk1
ajk1 on 19 Apr 2015
Edited: ajk1 on 20 Apr 2015
Hi, thank you for your comments. Sorry for the inconvenience. I hope this clarifies what I am trying to implement.
d= 0.2433, 0.2226, 0.2486, 0.2861, 0.0673
d0=mean(d)
d0=0.2433
Is d<=do? For elements where condition is true then evaulate statement 'final' to calculate fraction of value of d divided by sum of values of when condition is true.
Else d>do. For elements where d>do evaulate statement 'final' to calculate fraction of value of d divided by sum of values of values in instances where d>d0.
After this the result in final_table = 0.4563,0.4175, 0.4649, 0.5351, 0.1262
Result is (5x1) array, now repeat 5 times, with each successive iteration being stored in a new column of final_table. Final result (5x5) array. Thanks
Ajk1 - note that find will return the linear indices of the elements in some vector that satisfy some condition. So your line of code
d_pos_N1=find(d>d0);
is an array of indices. Is this intentional?
ajk1
ajk1 on 19 Apr 2015
It was unintentional. I noticed it after I debugged the program. Thanks for highlighting this.
I get
> d= [0.2433, 0.2226, 0.2486, 0.2861, 0.0673];
>> mean(d)
ans =
0.2136
>>
as starters, plus your code snippet produces
>> d= [0.2433, 0.2226, 0.2486, 0.2861, 0.0673];
>> mean(d)
ans =
0.2136
>> res
res =
0.2432 0.2225 0.2485 0.2859 1.0000
>> final_table(1,:)
ans =
0.3333 0.3333 0.3333 0.3333 5.0000
>>
which is owing to using find where (I presume and it appears from the above comment and response isn't what's wanted). What IS a correct response consistent with a set of correct inputs as requested?
I don't follow from the description what the successive iteration is intended to be iterating over, specifically.
ajk1
ajk1 on 20 Apr 2015
Hi, sorry when I did this on Matlab I didn't include the brackets, you are indeed correct.
d= [0.2433, 0.2226, 0.2486, 0.2861, 0.0673]
d0=mean(d)
d0=0.2136
and your result (res) is correct.
ajk1
ajk1 on 20 Apr 2015
Edited: ajk1 on 20 Apr 2015
The number of iterations cnt=numel(d), in this case 5 so result in final_table is nxn (to integrate with a later part of the code). In my code I have 100 elements hence why cnt=1:100 in my post. In this example since we have 5 elements I would like the result 5x5. Thanks.
dpb
dpb on 20 Apr 2015
So again, what IS the desired output for the full array consistent with a set of correct inputs as requested?
I don't follow from the description what the successive iteration is intended to be iterating over, specifically.
ajk1
ajk1 on 20 Apr 2015
I am interested to know how you implemented the if statement to carry out this function and keep the result in the same order. This example is for 5 elements but when I try to do it for 100 elements I only get half the results.
ajk1
ajk1 on 20 Apr 2015
Yes the desired output is consistent with the inputs. I have edited my post for the iterations.
Are you sure addition is what you want in your last lime... It seemse like it should be a concatenation of the system. If each matrix (final_N0 and final_N1) are the same length(ie 50) then you are adding the elements together and getting an output of 50 elements where as you would want to concatenate to get a column vector 100 units long
ajk1
ajk1 on 20 Apr 2015
Thanks for your comment, yes that is what I am trying to implement but not sure how to.
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

Asked:

on 19 Apr 2015

Commented:

dpb
on 21 Apr 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!