Stuck using For-loops

1 回表示 (過去 30 日間)
Tyee
Tyee 2014 年 3 月 8 日
編集済み: Jacques 2014 年 3 月 8 日
I am completely new to Matlab and I am stuck. Hopefully someone will be able to help me. I'm trying to run a loop so I don't have to write repetitive lines of code. Below is a sample of my loop.
for k=0:5
count000_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.000 & xyz_values_k(:,2)<0.025));
count025_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.025 & xyz_values_k(:,2)<0.050));
count050_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.050 & xyz_values_k(:,2)<0.075));
count075_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.075 & xyz_values_k(:,2)<0.100));
count100_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.100 & xyz_values_k(:,2)<0.125));
count125_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.125 & xyz_values_k(:,2)<0.150));
count150_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.150 & xyz_values_k(:,2)<0.175));
count175_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.175 & xyz_values_k(:,2)<0.200));
count200_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.200 & xyz_values_k(:,2)<0.225));
count225_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.225 & xyz_values_k(:,2)<0.250));
count250_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.250 & xyz_values_k(:,2)<0.275));
count275_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.275 & xyz_values_k(:,2)<0.300));
count300_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.300 & xyz_values_k(:,2)<0.325));
count325_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.325 & xyz_values_k(:,2)<0.350));
count350_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.350 & xyz_values_k(:,2)<0.375));
count375_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.375 & xyz_values_k(:,2)<0.400));
count400_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.400 & xyz_values_k(:,2)<0.425));
count425_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.425 & xyz_values_k(:,2)<0.450));
count450_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.450 & xyz_values_k(:,2)<0.475));
count475_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.475 & xyz_values_k(:,2)<0.500));
count500_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.500 & xyz_values_k(:,2)<0.525));
count525_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.525 & xyz_values_k(:,2)<0.550));
count550_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.550 & xyz_values_k(:,2)<0.575));
count575_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.575 & xyz_values_k(:,2)<0.600));
count600_k = numel(xyz_values_k(xyz_values_k(:,2)>=0.600 & xyz_values_k(:,2)<0.625));
count_k = [count000_k, count025_k, count050_k, count075_k, count100_k, count125_k, count150_k, count175_k, count200_k, count225_k, count250_k, count275_k, count300_k, count325_k, count350_k, count375_k, count400_k, count425_k, count450_k, count475_k, count500_k, count525_k, count550_k, count575_k, count600_k];
bar(count_k);
end
I'm having difficulties replacing the 'k's inside the code with the numeric values of the initial vector. If for-loops can't solve this problem, are there other options?

採用された回答

Jacques
Jacques 2014 年 3 月 8 日
編集済み: Jacques 2014 年 3 月 8 日
use count000 = zeros(5,1); which declares a vector of 5 elements initialized at 0. Now, you can loop:
for i=1:5
count000(i) = blablabla...
end
And make sure that xyz_values(:,2) is also a vector, by declaring xyz_values to be a 3dimensional matrix instead of 2D.
Even better, now you can keep only one variable for all count000, count025, etc... by using
count = zeros(5,22);
for i=1:5
for j=1:22
count(i,j) = blablabla... >= (j-1)*0.025 blablabla....
end
bar(i,:)
end
The (j-1)*0.025 gives you 0.00, 0.025, 0.050 etc as appropriate. By using two loops, you can reduce the size of the code pretty much :)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by