I'm having a hard time figuring out the for loop command. I want to create a .m file that will prompt a user to enter an array and then be able to identify positive and negative numbers or numbers equal to zero. But I'm not sure how to create the for loop command for this.
I know how to get the prompts in the program file but that's about all I'm getting to. I've been reading through a Matlab book and searching online but with no success. It's hard to teach yourself this stuff!
An example of what I would like to do is:
input('Enter the number of elements in a vector: ') v=input('Enter the array: ')
Now all I would like to do is set this up. I know my commands would be something like:
v<0 and v>0 % to get my negative and positive numbers
and v=0: to get what ever is equal to zero.
But how would I set this up using a for loop?

 採用された回答

per isakson
per isakson 2012 年 7 月 11 日
編集済み: per isakson 2012 年 7 月 11 日

0 投票

No need for a loop:
positive_numbers = v(v>0);
negative_numbers = v(v<0);
zero_numbers = v(v==0);
.
See "Logical indexing"
.
--- Example based on Comment 1 and 2 ---
In your code in the comment you overwrite the variables with identical results a number of times. I meant "No loop needed".
I misunderstood your question. I thought you wanted separate the positive values from the negative values. DOUBLE in the example below communicates the intent. Matlab doesn't need it.
Run this code as is (without a loop)
v = [ 1, 5, -3, 0 ];
positive_numbers = v(v>0);
negative_numbers = v(v<0);
zero_numbers = v(v==0);
numbers_of_positives = sum( double( v>0 ) );
numbers_of_negatives = sum( double( v<0 ) );
numbers_of_zeros = sum( double( v==0 ) );
This certainly doesn't serve as an exercise with loops, but it returns a result.

5 件のコメント

Aaron
Aaron 2012 年 7 月 11 日
編集済み: Aaron 2012 年 7 月 11 日
Here is an example of what I've been getting using an array of [1,5,-3,0]:
Number of positive elements = 1Number of positive elements = 5Number of negative elements = -3Number of elements equal to zero = 0
The identities are correct but I wanted to display the quantity and not the values.
per isakson
per isakson 2012 年 7 月 11 日
It is possible to mark-up the comments
per isakson
per isakson 2012 年 7 月 11 日
I assume that "%G" should read "%G\n"
Image Analyst
Image Analyst 2012 年 7 月 11 日
It's also possible now to edit comments, so you don't have two in a row, which is good since it only normally displays the last 3 comments and combining two would let one earlier one be seen that would otherwise be hidden.
Aaron
Aaron 2012 年 7 月 11 日
Awesome. Thank you.

サインインしてコメントする。

その他の回答 (2 件)

Luffy
Luffy 2012 年 7 月 11 日
編集済み: Luffy 2012 年 7 月 11 日

1 投票

sprintf('Enter elements of vector');
% after entering v,run below code
for i = 1:length(v)
if v(i) > 0
sprintf('v(%i)>0',i)
elseif v(i) <0
sprintf('v(%i)<0',i)
else
sprintf('v(%i)=0',i)
end
end
quaza mouinuddin mohammad
quaza mouinuddin mohammad 2020 年 1 月 4 日

0 投票

sprintf('Enter elements of vector');
% after entering v,run below code
for i = 1:length(v)
if v(i) > 0
sprintf('v(%i)>0',i)
elseif v(i) <0
sprintf('v(%i)<0',i)
else
sprintf('v(%i)=0',i)
end
end

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by