Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Need help 'while' loop, a data set is given?

Subject: Need help 'while' loop, a data set is given?

From: J K

Date: 27 Apr, 2013 16:11:08

Message: 1 of 12

Now I calculate the value of F from equation. From the F that I determined, I need to find the diameter from set of data.

diameter F
3 80
5 100
7 120
9 140
11 160

The method is like this. If my F value is smaller than the value of F from the data, then I choose the corresponding diameter.

For example. The value of F that I calculated is 11. The value of F from the data is 80, 100, 120, 140, 160. The first value is 80, final value is 160, and its corresponding diameter is 3,5,7,9,11.

110 is bigger than 80, the first value of the data, hence we move to the next F on the data. Again, 110 is bigger 100, we move to the next F.

But 110 is less than 120, the iteration is stop. We need not look further. We take the diameter of 120, which is 7. (Note: I don't need to do the interpolation process)

Thank you in advance.

Subject: Need help 'while' loop, a data set is given?

From: dpb

Date: 27 Apr, 2013 22:43:53

Message: 2 of 12

On 4/27/2013 11:11 AM, J K wrote:
> Now I calculate the value of F from equation. From the F that I
> determined, I need to find the diameter from set of data.
>
> diameter F
> 3 80
> 5 100
> 7 120
> 9 140
> 11 160
>
> The method is like this. If my F value is smaller than the value of F
> from the data, then I choose the corresponding diameter.
>
> For example. The value of F that I calculated is 11. The value of F from
> the data is 80, 100, 120, 140, 160. The first value is 80, final value
> is 160, and its corresponding diameter is 3,5,7,9,11.
>
> 110 is bigger than 80, the first value of the data, hence we move to the
> next F on the data. Again, 110 is bigger 100, we move to the next F.
>
> But 110 is less than 120, the iteration is stop. We need not look
> further. We take the diameter of 120, which is 7. (Note: I don't need to
> do the interpolation process)
>

yourd=d(find(yourF>F,1,'first'));

W/o error checking, of course...you don't say what to do on the ends so
salt to suit.

--

Subject: Need help 'while' loop, a data set is given?

From: dpb

Date: 27 Apr, 2013 22:54:05

Message: 3 of 12

On 4/27/2013 5:43 PM, dpb wrote:
> On 4/27/2013 11:11 AM, J K wrote:
>> Now I calculate the value of F from equation. From the F that I
>> determined, I need to find the diameter from set of data.
>>
>> diameter F
>> 3 80
...

>>
>> The method is like this. If my F value is smaller than the value of F
>> from the data, then I choose the corresponding diameter.
...

>
> yourd=d(find(yourF>F,1,'first'));
>
> W/o error checking, of course...you don't say what to do on the ends so
> salt to suit.

yourd=d(find(yourF>=F,1,'first'));

Actually, that gives the next larger, not smaller. Hmmm...thought it
was simpler than interp1 w/ 'nearest' option otomh but not so. But at
110 interp1 also returns the higher instead of lower so right now I
don't have a way w/o a test/branch...unless you can accept the next
larger for the halfway point, then

interp1(tableD,tableF,yourF,'nearest')

Fiddle around w/ the above and as above fixup to your desires...

--

Subject: Need help 'while' loop, a data set is given?

From: J K

Date: 28 Apr, 2013 02:44:08

Message: 4 of 12


 yourd=d(find(yourF>F,1,'first'));


When using the code, I get this error.

??? Error using ==> find
Second argument must be a positive scalar integer.

I don't get it.

Subject: Need help 'while' loop, a data set is given?

From: J K

Date: 28 Apr, 2013 03:12:07

Message: 5 of 12

"J K" wrote in message <kli2do$k2u$1@newscl01ah.mathworks.com>...
>
> yourd=d(find(yourF>F,1,'first'));
>
>
> When using the code, I get this error.
>
> ??? Error using ==> find
> Second argument must be a positive scalar integer.
>
> I don't get it.

This is my code . Before finding the value of diameter, it must have a requirement. And that's V is equal to zero. Then I use while loop.


while V==0
    Bore=Bore_3(find(C10>C10_3,'1'));
end


But I get the error.
??? Error using ==> find
Second argument must be a positive scalar integer.


If I replace the 'first' with '1', the matlab won't response. It just do nothing. Even the 'clear' command also didn't help. I have to start task manager to close the Matlab and open it again.

Subject: Need help 'while' loop, a data set is given?

From: dpb

Date: 28 Apr, 2013 04:24:31

Message: 6 of 12

On 4/27/2013 9:44 PM, J K wrote:
>
> yourd=d(find(yourF>F,1,'first'));
>
>
> When using the code, I get this error.
>
> ??? Error using ==> find
> Second argument must be a positive scalar integer.

Something in what you didn't show...

 >> [d F]
ans =
      3 80
      5 100
      7 120
      9 140
     11 160
 >> yourF=109;d(find(yourF>=F,1,'last'))
ans =
      5
 >> yourF=110;d(find(yourF>=F,1,'last'))
ans =
      5
 >> yourF=120;d(find(yourF>=F,1,'last'))
ans =
      7
 >>

--

Subject: Need help 'while' loop, a data set is given?

From: J K

Date: 28 Apr, 2013 07:17:10

Message: 7 of 12

Thank You

Subject: Need help 'while' loop, a data set is given?

From: J K

Date: 28 Apr, 2013 07:50:09

Message: 8 of 12

I would really appreciate if you can show me this as well. Basicaly it is related to the question above.

load data.dat;
d=data(:,1);
Fc=data(:,2);

m=input('Enter value of m:');
a=input('Enter value of a:');
C1=input('Enter the value of C1: ');
C2=input('Enter the value of C2: ');

if C2==0
    fprintf('it is V belt \n');
    F=m*a;
    
    while C2==0
    d(find(F<=Fc,'1','first'));
    end

end

Ok, I save the d and Fc data as 'dat' file.
m=55, a=2, F=110

The problem that I get is the command window cannot execute the while loop. It shows 'it is V belt'. Then the command window will not respond to you. Even clear command also cannot delete the info.

How to solve?

Subject: Need help 'while' loop, a data set is given?

From: dpb

Date: 28 Apr, 2013 14:02:00

Message: 9 of 12

On 4/28/2013 2:50 AM, J K wrote:
...

> C1=input('Enter the value of C1: ');
> C2=input('Enter the value of C2: ');
>
> if C2==0
> fprintf('it is V belt \n');
> F=m*a;
> while C2==0
> d(find(F<=Fc,'1','first'));
> end
> end
>
...

> The problem that I get is the command window cannot execute the while
> loop. It shows 'it is V belt'. Then the command window will not respond
> to you.


...

No, the problem is it _DOES_ execute the WHILE; you've made an infinite
loop. As there's nothing inside the WHILE...END that will ever change
the value of C2, the condition C2==0 if true to be in the IF to begin
with will never, ever become false.

As seen above, you don't need WHILE at all to solve the problem, the
search across the values of your vector is being done by FIND
automagically--that's the point of using it.

doc while
doc find

doc matlab % right under the big red 'MATLAB' press "Getting Started"

--

Subject: Need help 'while' loop, a data set is given?

From: J K

Date: 28 Apr, 2013 14:34:11

Message: 10 of 12


> As seen above, you don't need WHILE at all to solve the problem, the
> search across the values of your vector is being done by FIND
> automagically--that's the point of using it.
>
> doc while
> doc find
>
> doc matlab % right under the big red 'MATLAB' press "Getting Started"


Thank you. I got it.

Subject: Need help 'while' loop, a data set is given?

From: Steven_Lord

Date: 29 Apr, 2013 18:02:52

Message: 11 of 12



"dpb" <none@non.net> wrote in message news:klhkua$69i$1@speranza.aioe.org...
> On 4/27/2013 5:43 PM, dpb wrote:
>> On 4/27/2013 11:11 AM, J K wrote:
>>> Now I calculate the value of F from equation. From the F that I
>>> determined, I need to find the diameter from set of data.
>>>
>>> diameter F
>>> 3 80
> ...
>
>>>
>>> The method is like this. If my F value is smaller than the value of F
>>> from the data, then I choose the corresponding diameter.
> ...
>
>>
>> yourd=d(find(yourF>F,1,'first'));
>>
>> W/o error checking, of course...you don't say what to do on the ends so
>> salt to suit.
>
> yourd=d(find(yourF>=F,1,'first'));
>
> Actually, that gives the next larger, not smaller.

Assuming yourF is sorted ascending:

d(find(yourF <= F, 1, 'last'))

Or subtract yourF from F and find the value that MINimizes that difference
while remaining positive.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Need help 'while' loop, a data set is given?

From: dpb

Date: 29 Apr, 2013 19:22:31

Message: 12 of 12

On 4/29/2013 1:02 PM, Steven_Lord wrote:
...

>>> W/o error checking, of course...you don't say what to do on the ends so
>>> salt to suit.
>>
>> yourd=d(find(yourF>=F,1,'first'));
>>
>> Actually, that gives the next larger, not smaller.
>
> Assuming yourF is sorted ascending:
>
> d(find(yourF <= F, 1, 'last'))
...

Yeah, it dawned on me on followup posting to recast the test from the
first blush one, Steven. The followups do use the above that meets OP's
reqm't.

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us