Storing values calculated in a 'for' loop in a matrix / vector

I would very much appreciate some help storing values in a matrix. I am trying to do a growth curve (See: http://kellymom.com/images/growth/growthcharts.gif). For that reason I have to do quantile equations for a set of data (age and height). Currently, I've made a 'for' loop, which does calculate the value as intended, however it only stores the last result. How do I make it store the data as a matrix like:
Quantile 25 %
Age Height
  • 6 123
  • 7 129
  • 8 135
and so forth.
Thank you very much!
Note: male.data consists of two columns. First column is age and second is height of two thousand male participants. The file: age.height.mat is my data.
[EDITED, Jan, contents of the M-file from dropbox:]
%%Assignment 1 - Growth curves for men and women
clear all ;
clc ;
load age.height.mat
% Sorting the data to be plotted
male.data = sortrows(male.data) ;
plot(male.data(:,1),male.data(:,2),'r+')
male.growth = zeros(1, 73);
t = zeros(1, 73);
for k = 6:78
male.growth(k-5) = male.data(male.data(:,1) == k, 2);
t(k-5) = quantile(male.growth(k-5), 0.5);
end
% Note that t sometimes returns 'not a number', when a specific age is not
% present. That won't be a problem in my final script, as I am not allowed
% to publish all my data for copyright reasons.

4 件のコメント

Jan
Jan 2013 年 3 月 13 日
This forum does not have a strict text limit and postimng the code would be a really good idea.
Tobias
Tobias 2013 年 3 月 13 日
編集済み: Tobias 2013 年 3 月 13 日
Thank you for editing my post. I will just sort the layout a bit, as most of my comments in the script are probably not necessary.
Tobias
Tobias 2013 年 3 月 14 日
Anybody who can help me with fixing the dimension problem I am currently having?
"In an assignment A(I) = B, the number of elements in B and I must be the same."
male.growth(k-5) = male.data(male.data(:,1) == k, 2);
Tobias
Tobias 2013 年 3 月 15 日
I would just like to add that I have solved my problem! So thank you for inspirational input
This can be closed.

サインインしてコメントする。

 採用された回答

Jan
Jan 2013 年 3 月 13 日
編集済み: Jan 2013 年 3 月 13 日

0 投票

Please do not start your code with the cruel killer. There is no benefit in removing all loaded functions from the memory by clear all. If you have a good reason to clear all variables, use clear variables or even better use a function, because it has its own workspace.
For storing the results in a vector:
male.growth = zeros(1, 73); % Pre-allocate!!! Optional but recommended
t = zeros(1, 73);
for k = 6:78 % Avoid using "i" as a variable
male.growth(k-5) = male.data(male.data(:,1) == k, 2); % Without FIND
t(k-5) = quantile(male.growth(k-5), 0.5);
end

4 件のコメント

Tobias
Tobias 2013 年 3 月 13 日
Alright, I'll make sure to keep that in mind.
While I get what you're doing, I'm getting an error in the line:
male.growth(k-5) = male.data(male.data(:,1) == k, 2); % Without FIND
"In an assignment A(I) = B, the number of elements in B and I must be the same."
As I'm seeing it, the number of elements correspond just fine.
Jan
Jan 2013 年 3 月 13 日
So start the debugger:
dbstop if error
Then run the code again until it stops at the error. Then type in the command window:
a = male.data(male.data(:,1) == k, 2)
size(a)
Tobias
Tobias 2013 年 3 月 13 日
編集済み: Tobias 2013 年 3 月 13 日
After having debugged and writing the code you suggested, I got the following:
K>> size(a)
ans =
104 1
Which, as far as I understand, should correspond correctly with the 104 boys at the age of 6 included in my data. Even though the dimension of male.growth is 1 x 73 after the code:
male.growth = zeros(1, 73);
Isn't it supposed to automaticly change the dimensions of male.growth to the dimensions of the result in:
male.growth(k-5) = male.data(male.data(:,1) == k, 2);
Jan
Jan 2013 年 3 月 18 日
@Tobias: While for:
male.growth = male.data(male.data(:,1) == k, 2);
the dimensions on the left side are adjusted automatically, as for:
male.growth(100) == 8;
the missing elements are set to implicitely, a vector cannot assigned to a scalar:
a(1) = 1:10; % ERROR

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSpline Postprocessing についてさらに検索

製品

質問済み:

2013 年 3 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by