Filling vector with a condition.
28 ビュー (過去 30 日間)
古いコメントを表示
Hi.
I have two vectors A and B of the same length
I wan´t to create a new vector containing the same numbers of B, but only if the same element in A was a number>0, if not, i want to put a zero in that element.
How can i do this? Could you help me?
Thank you in advance!!
0 件のコメント
採用された回答
Stephan M. H.
2013 年 5 月 9 日
編集済み: Stephan M. H.
2013 年 5 月 9 日
Hi Javier,
as far as I understood your question you want to create a new vector with
for i=1:lenght(A)
if A(i)>0
C(i) = B(i)
end
end
This could be done in with the for loop above, but the fastest way should be via logical indexing.
C = zeros(size(A));
flag_a = A >0 % is logical 1 where elements are bigger zero
C(flag_a) = B(flag_a);
best, Stephan
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!