声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 3498|回复: 8

[绘图技巧] 如何实现描点编号

[复制链接]
发表于 2007-9-23 00:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

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

使用道具 举报

发表于 2007-9-23 08:46 | 显示全部楼层


非要不使用循环的话,help arrayfun
发表于 2007-9-23 09:11 | 显示全部楼层

回复 #2 eight 的帖子

我用的是7.0,里边没有arrayfun函数。所以如果楼主用的也是这个版本,或者更低的版本。可能还需要加循环
 楼主| 发表于 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.

    [A, B, ...] = 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.

    [A, ...] = 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 [A, B] = 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 =
               [1x4 double]  [1x8 double] [1x5 double]
       sMean = arrayfun(@(x)(mean(x.f1)), s, 'UniformOutput', false)
       sMean =
               [1x4 double]    [1x8 double]    [1x5 double]

       See also  cellfun, structfun, function_handle, cell2mat, spfun


    Reference page in Help browser
       doc arrayfun
发表于 2007-9-27 19:58 | 显示全部楼层
原帖由 gerry 于 2007-9-27 19:48 发表
我用的是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 编辑 ]
 楼主| 发表于 2007-9-27 20:04 | 显示全部楼层
哈,顿悟了……
代码如下

>> s=[1:10];
>> arrayfun(@(x)text(x,x,num2str(x)),s)
>> axis([0,10,0,10]);  

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

评分

1

查看全部评分

发表于 2007-9-27 21:22 | 显示全部楼层
原帖由 gerry 于 2007-9-27 20:04 发表
哈,顿悟了……
代码如下

>> s=[1:10];
>> arrayfun(@(x)text(x,x,num2str(x)),s)
>> axis([0,10,0,10]);  

多谢斑竹点拨:@D
不过奇怪的是输入第二行后,出来的图只是(0,1,0,1)的,加了下面那 ...

没有什么可奇怪的,看一下text的帮助文档就明白了。
 楼主| 发表于 2007-9-28 12:39 | 显示全部楼层
开始以为觉得不用循环程序会简单些,但用arrayfun函数实现后反倒绕远了

比较
tic
s=[1:10];
arrayfun(@(x)text(x,x,num2str(x)),s)
axis([0,10,0,10]);  
t1=toc

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

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

评分

1

查看全部评分

发表于 2007-9-28 12:53 | 显示全部楼层
原帖由 gerry 于 2007-9-28 12:39 发表
开始以为觉得不用循环程序会简单些,但用arrayfun函数实现后反倒绕远了

比较
tic
s=[1:10];
arrayfun(@(x)text(x,x,num2str(x)),s)
axis([0,10,0,10]);  
t1=toc

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


不奇怪,从 2006 版本开始 matlab 在循环方面已经改进许多,特别是在循环次数小的场合,不用循环也许代价更高,呵呵
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-2 21:22 , Processed in 0.234844 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表