gerry 发表于 2007-9-23 00:02

如何实现描点编号

用plot绘出5个点 plot(,,'X');
如何在坐标旁顺序添加名称1,2,3,4,5
text(,, ??)
不使用循环……

eight 发表于 2007-9-23 08:46

原帖由 gerry 于 2007-9-23 00:02 发表 http://www.chinavib.com/forum/images/common/back.gif
用plot绘出5个点 plot(,,'X');
如何在坐标旁顺序添加名称1,2,3,4,5
text(,, ??)
不使用循环……

非要不使用循环的话,help arrayfun

花如月 发表于 2007-9-23 09:11

回复 #2 eight 的帖子

我用的是7.0,里边没有arrayfun函数。所以如果楼主用的也是这个版本,或者更低的版本。可能还需要加循环

gerry 发表于 2007-9-27 19:48

我用的是2007a,倒是有这个函数,不过没看出来它和做图有什么关系?
斑竹的意思还是可以用 Plot做的么?


>>help arrayfun
ARRAYFUN Apply a function to each element of an array.
    A = ARRAYFUN(FUN, B) applies the function specified by FUN to each
    element of array B, and returns the results in array A.A is
    the same size as B, and the (I,J,...)th element of A is equal to
    FUN(C(I,J,...)). FUN is a function handle to a function that takes one
    input argument and returns a scalar value. FUN must return values of
    the same class each time it is called.The inputs must be of the
    following types:numeric, logical, char, struct, cell.The order in
    which ARRAYFUN computes elements of A is not specified and should not
    be relied on.

    If FUN is bound to more than one built-in or M-file, (that is, it
    represents a set of overloaded functions), then ARRAYFUN follows
    MATLAB dispatching rules in calling the function.

    A = ARRAYFUN(FUN, B, C,...) evaluates FUN using elements of arrays B,
    C,... as input arguments.The (I,J,...)th element of A is equal to
    FUN(B(I,J,...), C(I,J,...), ...).B, C, ... must all have the same size.

    = ARRAYFUN(FUN, C, ...), where FUN is a function handle to
    a function that returns multiple outputs, returns arrays A, B, ...,
    each corresponding to one of the output arguments of FUN.ARRAYFUN
    calls FUN each time with as many outputs as there are in the call to
    ARRAYFUN.FUN may return output arguments having different classes,
    but the class of each output must be the same each time FUN is called.

    = ARRAYFUN(FUN, B,..., 'Param1', val1, ...) enables you to
    specify optional parameter name/value pairs.Parameters are:

       'UniformOutput' -- a logical value indicating whether or not the
       output(s) of FUN can be returned without encapsulation in a cell
       array. If true (the default), FUN must return scalar values that can
       be concatenated into an array.If true, the outputs must be of the
       following types:numeric, logical, char, struct, cell.If false,
       ARRAYFUN returns a cell array (or multiple cell arrays), where the
       (I,J,...)th cell contains the value FUN(C(I,J,...), ...).When
       'UniformOutput' is false, the outputs can be of any type.

       'ErrorHandler' -- a function handle, specifying the function
       MATLAB is to call if the call to FUN fails.   The error handling
       function will be called with the following input arguments:
         -a structure, with the fields:"identifier", "message", and
            "index", respectively containing the identifier of the error
            that occurred, the text of the error message, and the linear
            index into the input array(s) at which the error occurred.
         -the set of input arguments at which the call to the function
            failed.

       The error handling function should either rethrow an error, or
       return the same number of outputs as FUN.These outputs are then
       returned as the outputs of ARRAYFUN.If 'UniformOutput' is true,
       the outputs of the error handler must also be scalars of the same
       type as the outputs of FUN. Example:

       function = errorFunc(S, varargin)
         warning(S.identifier, S.message); A = NaN; B = NaN;

       If an error handler is not specified, the error from the call to
       FUN will be rethrown.

    Examples
       Create a structure array with random matrices in the field f1.
       s(1).f1 = rand(7,4) * 10;
       s(2).f1 = rand(3,8) * 10;
       s(3).f1 = rand(5,5) * 10;

       Now compute the max and mean of each row of each matrix.
       sMax = arrayfun(@(x)(max(x.f1)), s, 'UniformOutput', false)
       sMax =
               
       sMean = arrayfun(@(x)(mean(x.f1)), s, 'UniformOutput', false)
       sMean =
                      

       See alsocellfun, structfun, function_handle, cell2mat, spfun


    Reference page in Help browser
       doc arrayfun

eight 发表于 2007-9-27 19:58

原帖由 gerry 于 2007-9-27 19:48 发表 http://www.chinavib.com/forum/images/common/back.gif
我用的是2007a,倒是有这个函数,不过没看出来它和做图有什么关系?
斑竹的意思还是可以用 Plot做的么?


>>help arrayfun
ARRAYFUN Apply a function to each element of an array.
    A = ARRAYFUN(F ...

arrayfun + text

如果对 arrayfun 不了解,可以看看 rocwoods 写的相关帖子

[ 本帖最后由 eight 于 2007-9-27 20:00 编辑 ]

gerry 发表于 2007-9-27 20:04

哈,顿悟了……
代码如下

>> s=;
>> arrayfun(@(x)text(x,x,num2str(x)),s)
>> axis();

多谢斑竹点拨:@D
不过奇怪的是输入第二行后,出来的图只是(0,1,0,1)的,加了下面那行才看到其他点,不知道为什么

花如月 发表于 2007-9-27 21:22

原帖由 gerry 于 2007-9-27 20:04 发表 http://www.chinavib.com/forum/images/common/back.gif
哈,顿悟了……
代码如下

>> s=;
>> arrayfun(@(x)text(x,x,num2str(x)),s)
>> axis();

多谢斑竹点拨:@D
不过奇怪的是输入第二行后,出来的图只是(0,1,0,1)的,加了下面那 ...
没有什么可奇怪的,看一下text的帮助文档就明白了。

gerry 发表于 2007-9-28 12:39

开始以为觉得不用循环程序会简单些,但用arrayfun函数实现后反倒绕远了

比较
tic
s=;
arrayfun(@(x)text(x,x,num2str(x)),s)
axis();
t1=toc

tic
for i=1:10
    text(i,i,num2str(i)),hold on
end
axis();
t2=toc

t1 =    0.4551 ; t2 =    0.0555;
arrayfun函数功能强大,但在这里直接采用循环的效率还是要高很多,再次感谢两位斑竹的热心解答:handshake

eight 发表于 2007-9-28 12:53

原帖由 gerry 于 2007-9-28 12:39 发表 http://www.chinavib.com/forum/images/common/back.gif
开始以为觉得不用循环程序会简单些,但用arrayfun函数实现后反倒绕远了

比较
tic
s=;
arrayfun(@(x)text(x,x,num2str(x)),s)
axis();
t1=toc

tic
for i=1:10
    text(i,i,nu ...

不奇怪,从 2006 版本开始 matlab 在循环方面已经改进许多,特别是在循环次数小的场合,不用循环也许代价更高,呵呵
页: [1]
查看完整版本: 如何实现描点编号