Dear all, How to save the value of x vector in de vector based on find function?
expected results is : de = [1 4 7 3 10 6 90 1.1 80];
x=[ 1 4 7 3 10 6 0 90 1.1 80]; for i=1:10 de = find(x > 0); end
Many thanks...

 채택된 답변

Stephen23
Stephen23 2018년 8월 3일
편집: Stephen23 2018년 8월 3일

0 개 추천

Your loop is superfluous, get rid of it. Simpler with logical indexing:
>> x = [1,4,7,3,10,6,0,90,1.1,80];
>> de = x(x>0)
de =
1 4 7 3 10 6 90 1.1 80

댓글 수: 6

laith Farhan
laith Farhan 2018년 8월 3일
Dear Stephen,
Thanks a lot for your reply.
Is there any way to work with find function because I need to use find?
thanks
de = x(find(x>0))
This is simply less efficient than logical indexing.
laith Farhan
laith Farhan 2018년 8월 3일
Dear Stephen, Sorry for disturb you. I am looking for the location of elements. Could you help me please?
for i=1:10 x=[ 1 4 7 -3 10 6 0 -90 1.1 -80]; de = (find(x <= 0)); end expected results is: [ 0 0 0 4 0 0 7 8 0 10];
x=[ 1 4 7 -3 10 6 0 -90 1.1 -80];
de = (find(x <= 0));
z = zeros(size(x));
z(de) = de
Stephen23
Stephen23 2018년 8월 3일
편집: Stephen23 2018년 8월 3일
"expected results is: [ 0 0 0 4 0 0 7 8 0 10];"
With two vectors and a times operation:
>> x = [1,4,7,3,10,6,0,90,1.1,80];
>> (1:numel(x)) .* (x<=0)
ans =
0 0 0 4 0 0 7 8 0 10
If your professor insists that you use a pointless find, then something like this perhaps:
>> idx = find(x<=0);
>> vec = zeros(size(x));
>> vec(idx) = idx
vec =
0 0 0 4 0 0 7 8 0 10
laith Farhan
laith Farhan 2018년 8월 3일
Thanks alot that really helpful.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

태그

질문:

2018년 8월 3일

댓글:

2018년 8월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by