How can I write a function called triAreaN that computes the area of N triangles?

The input of the function is a 2xN array where each column of the array contains the base and height of a single triangle. The function returns a 1xN array where the i^th column is the area of the i^th triangle. Test whether the base and height are positive. if either one is negative, set the area of the triangle to zero.
function triAreaN(B1...Bi;H1...Hi);
%H = Height of triangle (B1...Bi)=('enter the base'); (H1...Hi)=('enter the height');
A = (1/2*(B1*H1):1/2*(B1*Bi)); A = ('display answer'); A(output) end
Line 1: invalid syntax at '<EOL>'. Possibly, a ),}, or ] is missing. Line 4: Terminate statement with semicolon to suppress output(in functions). Line 5: invalid syntax at '<EOL>'. Possibly, a ),}, or ] is missing. Line 8: The value assigned to variable A' might be unused.
Homework problem. he never showed us how to use matrices or array in function or how to assign the rows to be certain input values. Stuck here not sure where to go from this point.

1 件のコメント

Roger Stafford
Roger Stafford 2014 年 1 月 27 日
It would have been much more interesting if your inputs had been given as the 3D cartesian coordinates of the triangles' vertices rather than their base and height.

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

 採用された回答

Pedro Villena
Pedro Villena 2014 年 1 月 27 日
function A = triAreaN(B,H)
if numel(B)==numel(H),
A = 0.5*(B.*H);
else
error('B and H must be same length');
end

2 件のコメント

jacob
jacob 2014 年 1 月 27 日
編集済み: jacob 2014 年 1 月 27 日
It says unexpected matlab expression whenever i enter a matrix like triAreaN(1 2 3 ; 4 5 8).
AYUSH MISHRA
AYUSH MISHRA 2020 年 5 月 23 日
very bad solution

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

その他の回答 (1 件)

Amit
Amit 2014 年 1 月 27 日
function A = triAreaN(Ar)
% Ar is 2XN array where each column is base and height
A = 0.5*Ar(1,:).*Ar(2,:);
A(Ar(1,:) < 0 | Ar(2,:) < 0) = 0;

7 件のコメント

Amit
Amit 2014 年 1 月 27 日
After you save the code, you can run the code as
A = triAreaN(_your_input_)
jacob
jacob 2014 年 1 月 27 日
would my input be in array form?
Amit
Amit 2014 年 1 月 27 日
編集済み: Amit 2014 年 1 月 27 日
Yes. That is what the question states. Your input will be 2XN matrix (2 rows and N columns). Each column will be the height and base of the triangle. There can be negative values for either or both of them, in that case you need to show the result for those as 0.
I am sorry that your professor didn't teach you these. However, I'd also say that MATLAB has amazing help. Just type on google the problem keyword where you're stuck, you'll get something similar.
jacob
jacob 2014 年 1 月 27 日
I appreciate the help. From what i know you write matrices like (123;456) for example. Is that correct?
Amit
Amit 2014 年 1 月 27 日
Yup:
Ar = [1 2 3;4 5 6];
Here, 1 2 3 will be first row and 4 5 6 will be second.
jacob
jacob 2014 年 1 月 27 日
so would i type it in the command window or run it as triAreaN(1 2 3 ; 4 5 6) or does it need brackets? either way i do it it sayunexpected expression or unexpected parenthesis or bracket.
jacob
jacob 2014 年 1 月 27 日
nvm. i got it. thank you so much!

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2014 年 1 月 27 日

コメント済み:

2020 年 5 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by