How to write a function that returns the element that is the center of a vector or matrix?
古いコメントを表示
how do I start writing a function to do this? I have an idea how to write a script, but not as a function.
example:
123
458
112
ans=[2;5;1]
or
1233
ans=[2,3]
4 件のコメント
Azzi Abdelmalek
2014 年 4 月 17 日
What is 1233? double or string?
Kimberly
2014 年 4 月 17 日
Sven
2014 年 4 月 17 日
I think Azzi's question was just because the way you wrote the input:
[1 2 3 3]
is a row-vector, however:
[1223]
is just a number
Kimberly
2014 年 4 月 17 日
採用された回答
その他の回答 (1 件)
Hi Kimberly, try this:
% Set up some input
x = [1 2 3
4 5 8
1 1 2]
% Grab the middle column(s)
xMidVal = (size(x,2)+1) / 2;
midInds = unique([floor(xMidVal) ceil(xMidVal)]);
out = x(:,midInds)
Did that answer the question for you?
4 件のコメント
Kimberly
2014 年 4 月 19 日
Sven
2014 年 4 月 20 日
Kimberly, this answer will only return decimals (into the variable I have called out) if the input (in variable x) contains decimals. You can just copy into MATLAB and see that it returns exactly what you're looking for:
out =
2
5
1
It works similarly when you set x = [1 2 3 3]:
out =
2 3
Star Strider
2014 年 4 月 21 日
My sympathies, Sven.
Sven
2014 年 4 月 21 日
Ha, thanks SS :)
Kimberley, you are receiving quite detailed answers from Geoff who has (kindly) devoted time to helping you. I think that an hour or so reading some of the getting started guide will also be of much benefit. Two topics: Matrices and arrays and Programming and scripts would be great places to start.
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!