If statement for identifying row/column vectors/matrix/scalars

The question I am trying to solve, is how to create a function that recognizes the input given as either a colum vector, row vector, matrix, or scalar, depending on which category the input falls under it will execute simple arithmetic, that I can do. I have to create a function, myvec(x) that
the simple arithmetic I can execute, however, Im not sure how to type the if statements for the conditions the question prompts, for example, for the first condition how would I type
function y=myvec(x)
if y=rowvec? [1:n]? [1,n]?
or what is the correct way to do this, thank you

回答 (1 件)

Sudhakar Shinde
Sudhakar Shinde 2020 年 11 月 3 日
編集済み: Sudhakar Shinde 2020 年 11 月 3 日

1 投票

'isrow' function returns true if input is row vector.
isrow(in)
'iscolumn' function returns true if input is column vector.
iscolumn(in)
'ismatrix' function returns true if input is matrix.
ismatrix(in)
'isscalar' function returns true if input is scalar.
isscalar(in)

2 件のコメント

Sudhakar Shinde
Sudhakar Shinde 2020 年 11 月 3 日
example below
function y=myvec(x)
if isrow(x) %Check if row vector
ReveresRow = sort(x,'descend'); %Reverse elements
y = ReveresRow.^2; %Square
end
end
you can add else if conditions for column vector, matrix or scalar
Joseph DeMaria
Joseph DeMaria 2020 年 11 月 3 日
Awesome this is helping so much! Appreciate your guys help so much, so far for my functions code I have
function y=myvec(x)
if isrow(x)
flip(x)
y=x.^2
if iscolumn(x)
y=vec-.5
if ismatrix(x)
y=(x*(x')).^2
if isscalar(x)
y=x
end
end
end
end
end
is this right for such conditions the question prompts?

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

カテゴリ

ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索

質問済み:

2020 年 11 月 3 日

コメント済み:

2020 年 11 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by