How to write a function that returns the element that is the center of a vector or matrix?

28 次查看(过去 30 天)
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 个评论
Sven
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

请先登录,再进行评论。

采纳的回答

Geoff Hayes
Geoff Hayes 2014-4-17
Hi Kimberly,
Your function signature could look something like this:
function [ctr] = find_centre (x)
<code to be filled in by you!>
end
where x is your input (from your examples 1233 or [123;458;112]) and ctr is the found centre of the x (again from your examples, [2 3] or [2;5;1]).
I'm guessing that if your input is a matrix then the output should be the centre of each row?
Since you say that your input can be a vector or a matrix (a string of characters can be a vector so whether your input is a row of characters, numbers, cells, or whatever, the logic should be the same) then you probably want to handle the inputs separately by checking to see if x is a vector or is a matrix using the isvector and ismatrix functions respectively. (Note that a vector can be a matrix but not the other way around, so you will want to start with the vector check.)
If the input is a vector, then check its size/length using either of numel (number of elements) or length. The centre of your vector then will be dependent upon whether the size is even or odd (as you have shown in your examples) and you can then extract it from x and set it in ctr via the ctr = x(cstart:cstop); where cstart and cstop are the centre start and stop indices within the vector. From your example of 1223, then cstart=2 and cstop=3. If just 123, then cstart=2 and cstop=2 as well.
If the input is a matrix then you can get the number of rows and columns and do something similar to the above.
Geoff
  12 个评论
Geoff Hayes
Geoff Hayes 2014-4-21
All that the error is telling you is that you have not assigned the output variable cntr to anything. This variable should be your B in your above code. So just replace B with cntr or vice-versa.
You have a line of code
[a,c]=size(data);
which returns the rows (may want to chane a to r). This is good because from this you can determine the number of elements in the matrix or vector, call it n say where
n=r*c;
Now use this n in place of your call to length since that command will only work correctly for vectors and you want the count of all elements in the matrix and not just the number of elements along the largest dimension.
The assignments to cstart and cstop are not correct: you are taking the input matrix and dividing each element by 2 and so the start and stop variables are matrices! Instead replace with the number of elements in the matrix, n.
You may still want to handle the case of the input being a vector or a matrix because I'm not convinced the code will always work correctly for matrices (and I don't have access to MATLAB right now to test it out).
And please consider putting break points in your code and stepping through it to see what is happening for your different test cases.

请先登录,再进行评论。

更多回答(1 个)

Sven
Sven 2014-4-17
编辑:Sven 2014-4-17
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 个评论
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by