I have an array
vk = [0 0.025 0.05 0.075 0.1 0.125 0.15 0.175].
I am writing this line
op= vk(vk>= 0.05 & vk<= 0.175)
The result which I am getting is
op =
0.0750 0.1000 0.1250 0.1500 0.1750
which is wrong, I should get
0.05 0.0750 0.1000 0.1250 0.1500 0.1750
does anyone know what I am doing wrong?
Also how I can get the indices number of the array vk and of op?

댓글 수: 6

It worked for me:
vk = [0 0.025 0.05 0.075 0.1 0.125 0.15 0.175];
op = vk(vk>= 0.05 & vk<= 0.175)
>> vk = [0 0.025 0.05 0.075 0.1 0.125 0.15 0.175];
op= vk(vk>= 0.05 & vk<= 0.175)
op =
Columns 1 through 4
0.05 0.075 0.1 0.125
Columns 5 through 6
0.15 0.175
Chris Dan
Chris Dan 2020년 1월 11일
no, its not working on this
pic2.JPG
pic3.JPG
Can you tell why?
Stephen23
Stephen23 2020년 1월 11일
편집: Stephen23 2020년 1월 11일
"no, its not working on this ... Can you tell why?"
Can you explain why you think that it is not working?
Chris Dan
Chris Dan 2020년 1월 11일
the value 0.05 is not included in "op", it should be included as well, what change can be done?
Chris Dan
Chris Dan 2020년 1월 11일
편집: Chris Dan 2020년 1월 11일
I kind of got it,
op = vk(vk>= 0.05-0.00000000001 & vk<= 0.175)
subtracting a very small number, it is getting me the correct answer in my case...

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

 채택된 답변

Meg Noah
Meg Noah 2020년 1월 10일

0 개 추천

To get the indices to the vk array
vk = [0 0.025 0.05 0.075 0.1 0.125 0.15 0.175];
[idx] = find(vk>= 0.05 & vk<= 0.175);
op = vk(idx)

댓글 수: 4

find is not required, logical indexing is simpler and more efficient:
idx = vk>=0.05 & vk<=0.175;
op = vk(idx)
Chris Dan
Chris Dan 2020년 1월 11일
편집: Chris Dan 2020년 1월 11일
@Meg Noah It is not giving the index of the first value
Meg Noah
Meg Noah 2020년 1월 11일
편집: Meg Noah 2020년 1월 11일
The OP specifically asks: Also how I can get the indices number of the array vk and of op? And that is why I have a 'find' command.
When I run this code to find the indices of the vk meeting that condition, this is what is displayed:
>> idx = find(vk>=0.05 & vk<=0.175)
idx =
3 4 5 6 7 8
Remember: matlab arrays begin indexing at position 1. There is no 0th element.
>> vk(0)
Array indices must be positive integers or logical values.
The value of element 1 is 0. Since it does not meet the condition, the index 1 is not returned on the list of indices that meet the condition.
The value of element 2 is 0.025. Since it does not meet the condition, the index 2 is not returned on the list of indices that meet the condition.
The value of element 3 is 0.025. It is the first value that meets the condition, and index 3 is the first value returned on the list of indices that meet that condition.
If your problem is floating point, then maybe try recasting it. Is it being set to a double precision array initially?
vk = double(vk);
Chris Dan
Chris Dan 2020년 1월 12일
I kind of made this loop to get the indexes of vk
vk = [0 0.025 0.05 0.075 0.1 0.125 0.15 0.175];
ind= size(vk,2);
for i = 1:1:ind
index(i) = i
end
Because first element is zero so I wasnt getting the index of that one

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 1월 10일

댓글:

2020년 1월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by