Help with for loop.
显示 更早的评论
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 个评论
Geoff Hayes
2015-4-19
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
2015-4-19
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?
Geoff Hayes
2015-4-19
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
2015-4-19
dpb
2015-4-20
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
2015-4-20
dpb
2015-4-20
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
2015-4-20
ajk1
2015-4-20
Will Donahue
2015-4-20
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
2015-4-20
dpb
2015-4-21
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...
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!