Save values in a loop in a vector
7 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
Thorsten
2016-9-9
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 个评论
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).
另请参阅
类别
在 Help Center 和 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!