Save values in a loop in a vector

Hi !
I would need some help for my home assignment cause im stucked. I need to iterate thru a vector and look for some values that are equal to 1.00, 0.80, 0.60, 0.40, 0.20 and 0.10. Then i need to store those values in another vector, how do i do this? Below u see the code for plotting those but i need to save them all in a vector so that i can make a nice table!
for i=1:size(d)
if d(i)==1.00000
disp(h(i))
elseif d(i)==0.80000
disp(h(i))
elseif d(i)==0.60000
disp(h(i))
elseif d(i)==0.40000
disp(h(i))
elseif d(i)==0.20000
disp(h(i))
elseif d(i)==0.10000
disp(h(i))
end
end

回答 (2 件)

mbonus
mbonus 2016 年 9 月 8 日
編集済み: mbonus 2016 年 9 月 8 日

0 投票

before the loop
v = zeros(size(d));
then just insert this line for each part of the if structure
v(i) = d(i);
If you need to get rid of the indexes with no values you can do this after the loop
v(find(0)) = [];

9 件のコメント

mbonus
mbonus 2016 年 9 月 8 日
a faster way to code though would be to recreate the loop and write it once rather than in the if structure if all of d meets the criteria.
Daniel
Daniel 2016 年 9 月 8 日
Thank you! The problem is that i got a error saying:
Error using zeros Requested 25001x25001 (4.7GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become unresponsive. See array size limit or preference panel for more information.
mbonus
mbonus 2016 年 9 月 8 日
編集済み: mbonus 2016 年 9 月 8 日
What is the size of d? I may have messed up with the zeros(), try
zeros(size(d))
Daniel
Daniel 2016 年 9 月 8 日
its an 25001x1 vector =)
mbonus
mbonus 2016 年 9 月 8 日
I messed up with zeros(). does the corrected version work?
Daniel
Daniel 2016 年 9 月 9 日
Hi again!
This wasnt what i wanted. I just need to know the i`s so that i can find the corresponding value in another vector. So, the vector d contains different denstity values corresponding to different altitudes. So, im saying that, find the i`s for different values for p and then use the i`s to find the corresponding values in z :)
Daniel
Daniel 2016 年 9 月 9 日
So, now i have this:
H = []; Z = [];
for i=1:size(d)
if d(i)==1.00000
Z(i) = z(i);
H(i) = h(i);
elseif d(i)==0.80000
Z(i) = z(i);
H(i) = h(i);
elseif d(i)==0.60000
Z(i) = z(i);
H(i) = h(i);
elseif d(i)==0.40000
Z(i) = z(i);
H(i) = h(i);
elseif d(i)==0.20000
Z(i) = z(i);
H(i) = h(i);
elseif d(i)==0.10000
Z(i) = z(i);
H(i) = h(i);
end
end
H=find(H); Z=find(Z);
Daniel
Daniel 2016 年 9 月 9 日
and result i get is all i positions in vector d which is == to 1.00, 0.80 and so on. But what i need is to use those i`s and save the values in z(i) in a new vector Z
mbonus
mbonus 2016 年 9 月 12 日
Could post an example of what you get and what it should look like?

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

Thorsten
Thorsten 2016 年 9 月 9 日

0 投票

I found it a bit hard to understand what your are looking for. As far as I understood, this can solve your problem:
p = [1.00, 0.80, 0.60, 0.40, 0.20, 0.10];
for i = 1:numel(p) % for all values in p
Z{i} = z(h == p(i)); % find all positions in h that equal p(i), and assign the corresponding positions in z to a new variable Z
end

2 件のコメント

Daniel
Daniel 2016 年 9 月 9 日
For exampel: I have d=[1 22 31 41 51 6 7 8 9 10]; z=[1 2 3 4 5 6 7 8 9 10];
Now, i want to search in d for a value of 41. Then, give me that position, which in this case is on fourth column, hence i = 4
Now, go into the z vector and find me the value of column 4 and take that value and place it into Z vector :)
Thorsten
Thorsten 2016 年 9 月 9 日
編集済み: Thorsten 2016 年 9 月 9 日
Z = z(d==41)
In my code above d == h and p(i) == 41. Because there can be, depending on your data, in principle one, two, or even more matches for d== 41, you have to store one, or two, etc values in Z, i.e., a different number of values for each i. That's why I use Z{i}. If you can guarantee that there is always one and only one match for each i, you can use Z(i).

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2016 年 9 月 8 日

コメント済み:

2016 年 9 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by