Search Comments and Ratings

go

Comments and Ratings

   
Date File Comment by Comment Rating
25 Nov 2009 current_date prints out current date and time. Author: SamGhim Jos (10584)

Do no rely on this when you have to be on time ...
>> [d,t] = current_date
% d = 2009\11\25
% t =10:11:54
>> datestr(now)
ans = 25-Nov-2009 10:49:07

This trivial submission might have only some educational value, but than it should 1) work correctly; 2) have a lot of (edcucational) comments; and 3) a superior help section

I would recommend anyone to use DATESTR.

24 Nov 2009 'c' language style code for dilation A 'c' style code for dilation Author: Rama Chandra Rao Naradasu Jos (10584)

As long as you
- do not know how to optimize codes like this (in matlab, not in C!)
% -- begin of code --
for i=1:a
    for j=1:b
        y(i,j)=0;
        dia(i,j)=0;
     end
end
% -- end of code --

- do not know the advantages of pre-allocation
- do not the difference between functions and scripts
- uses clc and clear all without a good reason

your function is rather useless to others ...

19 Nov 2009 Truth Table Generation This function simply computes the truth table for a given number of variables. Author: Mahendra Jos (10584)

The pre-allocation and the use of logicals is certainly an improvement. Next try to improve the help section. See any standard function for inspiration how to set up such a comment section, for instance, DISP or MEAN. Also take a look at LOOKFOR to learn about H1 lines.

16 Nov 2009 Truth Table Generation This function simply computes the truth table for a given number of variables. Author: Mahendra Jos (10584)

This submission has several problems.
1. The help section is insufficient: add a H1 line, a clear description how to use this function and what to expect, and add an example.

2. The for-loop is not pre-allocated, and therefore quite slow with large n

3. There are several faster alternatives, for instance
n = 4 ;
R = rem(floor([0:((2^n)-1)].'* pow2(0:-1:-n+1)),2)
(adapted from DEC2BIN)

05 Nov 2009 Level crossings Detect level crossing of signals. Author: roberto madia Jos (10584)

Just curious what you mean by "This algorithm is generally faster than traditional ones". What are the traditonal ones?

I would suspect that an operation using something using DIFF and SIGN, as in (not-tested) "sum(diff(sign(Signal-OneLevel))) ~= 0)" is faster (as well as shorter)

05 Nov 2009 Allstats Many statistics of a vector Author: Francisco de Castro Jos (10584)

It is nice to see some improvments Franciso! A couple of additional comments: if your sor worried about speed, I suggest you take a look at logical indexing, and replace all the zeros from the struct command by one call to zeros.

Another point: the behavior for 2D matrices should be explained, especially when the groups are specified. I think you did not intend that behavior. For instance, take a look if the following produces what you want them to produce:

A = rand(3,4) ; allstats(A), allstats(A,1:4), allstats(A,1:3)

With respect to the help, you can add examples of the expected output, besides giving only the calling syntax.

Below is the outline of a more optimized code:
function R = allstats(DATA, GROUP)
if isempty(ver('stats'))
    error('Function requires STATS toolbox.') ;
end
if nargin==1,
    GROUP = ones(numel(DATA),1) ;
    Ngroups = 1 ;
    UniqueGroups = 1 ;
else
    if numel(DATA) ~= numel(GROUP)
        error('Number of elements should match') ;
    end
    UniqueGroups = unique(GROUP) ;
    Ngroups = numel(UniqueGroups) ;
end
Z = nan(Ngroups,1) ;
R = struct('min',Z,'max',Z,'mean',Z) ; % etce
for k=1:Ngroups,
    Q = GROUP == UniqueGroups(k) ; % logical indexing
    R(k).min = nanmin(DATA(Q)) ;
    R(k).max = nanmax(DATA(Q)) ;
    R(k).mean = nanmean(DATA(Q)) ;
end

04 Nov 2009 Allstats Many statistics of a vector Author: Francisco de Castro Jos (10584)

This submission can be improved considerably, considering the following:
1) when the data and group specification does not match, e.g., allstats([1 2 3],[1 2]), it should error
2) when the group data is not specified another piece of code is executed than when it is specified. Why not set the group data to all zeros, when no second argument is given. (and why not use nargin if the second argument is specified, instead of varargin)
3) Grouping does not work flawlessly: for instance ALLSTATS(rand(1,5),1:5) returns overall statistics, rather than statistics for the five individual groups
4) As Duane already mentioned, there should be reference, and some See Also's to the Stats TB and other functions

So, a lot to improve on this potential useful pick-of-the-week wrapper function.

03 Nov 2009 Counter. Counter. Hours, minutes and seconds. Author: pablo_zuniga Zúñiga Jos (10584)

Here are the first few lines of this script:
%Cronometro.
clc
clear
resp='s';
while (resp=='s')

clc
clear
a=input('Ingrese horas\n\n');
disp(' ')
b=input('Ingrese minutos\n\n');
disp(' ')
c=input('Ingrese segundos\n\n');
disp(' ')

Potential users should be aware that running this script does clear the entire workspace without notice (and it do so twice!), so all your work will be lost.
This script serves no purpose other than being a programming exercise for the author, so why share it here?

03 Nov 2009 Counter. Counter. Hours, minutes and seconds. Author: pablo_zuniga Zúñiga Jos (10584)

17 Sep 2009 Clear All But Clear the workspace while keeping some of your variables. Author: Roland Pfister Jos (10584)

Besides the existence of various KEEP functions, more recent versions of ML also know CLEARVARS, making this submission rather superfluous ...

10 Sep 2009 multisetdiff like setdiff, but any repeated elements of A are removed only once for each time they occur in B Author: Ben Petschel Jos (10584)

Please ignore my previous thought as it is incorrect !!

10 Sep 2009 multisetdiff like setdiff, but any repeated elements of A are removed only once for each time they occur in B Author: Ben Petschel Jos (10584)

Nice utility! Just a thought, for numerical vectors, this might be more convenient:

C = union(A,B) ;
res = C(histc(A,C) > histc(B,C))

To deal with rows of matrices or cell array strings, one could convert A and B into indices using, e.g., the third output of unique.

03 Sep 2009 NUMUNIQUE Returns unique elements in an array and all the indices for the repetitious values. Author: Zhigang Xu Jos (10584)

Nice implementation with good help (although missing a see also to UNIQUE). Here is another approach you might consider:

B = unique(A) ;
P = cell(1,numel(B)) ;
for k = 1:numel(B)
  P{k} = find(A==B(k)) ;
end

and in more recent ML versions:
P = arrayfun(@(x) find(x==A),B,'un',false)

28 Aug 2009 Permute a Matrix From given order of rearrangement constructs the permutation matrix P and computes B = P'*A*P Author: Andrey Popov Jos (10584)

I was a little sloppy .. I meant the second output ...

27 Aug 2009 Permute a Matrix From given order of rearrangement constructs the permutation matrix P and computes B = P'*A*P Author: Andrey Popov Jos (10584)

This function does not work (and rightfully so!) when 1) the input is not square or when idp does not contain a permutation of all N numbers between 1 and N (for instance [1 3 3]). However, such likely inputs lead to unfriendly error messages, so I suggest you add some error checks in your code.
Furthermore, P can be defined more easily as
  P = eye(size(A)) ;
  P = P(idp,:) ;
Finally, the help section is quite nice, but lacks a description of the third output. And, btw, is the copyright really needed given the BSD license. So, room for improvement.

08 Jul 2009 logical2cellstr, cellstr2logical Convert from a logical array to a cell array of 'true'/'false' strings and back again. Author: Richie Cotton Jos (10584)

No need for for-loops. In logical2cellstr:

c = cell(size(tf));
c(:) = {'false'} ;
c(tf) = {'true') ;

For cellstr2log, take a look at strcmpi or regexp ...

It was pleasure to find good help though with examples!

29 Jun 2009 divisor(n) Calculate the distinct divisors of a number not just the prime factors. Author: Yash Jos (10584)

For another implementation, see
http://www.mathworks.com/matlabcentral/fileexchange/3944

24 Jun 2009 EXPAND Replicate and tile each element of an array. Author: Matt Fig Jos (10584)

Why not simply
kron(A,eyes(SZ))

or, for non-numerical arrays,
X = {'a' 'b' ; [1:3] []}
X(kron(reshape(1:numel(X),size(X)), ones(2,3)))
%ans =
% 'a' 'a' 'a' 'b' 'b' 'b'
% 'a' 'a' 'a' 'b' 'b' 'b'
% [1x3 double] [1x3 double] [1x3 double] [] [] []
% [1x3 double] [1x3 double] [1x3 double] [] [] []

It can be expanded to the ND case using reshape etc ...

Good help though!

29 May 2009 rsplit Splits a delimited string into a cell array using a regular expression. Author: Gerald Dalley Jos (10584)

@Ged Ridgway

As suggested:

R = '\s+(and|&)\s+'
S= 'one and two & three'
Answer = strread(regexprep(S,R,' '),'%s').'

does the job ...

20 May 2009 choose.m compute number of ways of choosing m objects from n distinct objects Author: Phillip M. Feldman Jos (10584)

If it is all about speed, remove the error checks! For larger numbers the following formula is more appropriate:
exp(gammaln(N+1) - gammaln(K+1) - gammaln(N-K+1)) ;

18 May 2009 cell2num.m convert a cell array to a double precision array Author: Phillip M. Feldman Jos (10584)

See CELL2FLOAT
http://www.mathworks.com/matlabcentral/fileexchange/19730

for a more flexible implementation.

And who is the author of this submission? Vasi or Feldman?

11 May 2009 Oil Paint Effect This m file performs an Oil Paint effect on an input image. Author: Ahmed Hassan Jos (10584)

This is poor in several ways. First of all it is a script that starts with: "clc; clear all; in_img = imread('sofa.JPG'); .." Second, it lacks help and comments. Third, it is poorly written: for instance, not pre-allocation and avoidable loops. Finally, it turns color images into an BW oil-paint sketch ...
As it stands, this submission is useless to anyone but the author. Please learn how to write functions that are flexible and do not destroy the users workspace and that contains help!

06 May 2009 GRIDXY (v2.2 feb 2008) plot horizontal and vertical grid Author: Jos (10584) Jos (10584)

@Chiara. You can use, e.g.,
 h(1) = gridxy(MedianValue ,'color','r')
 h(2) = gridxy(MeanValue ,'color','b')
legend(h,{'Median','Mean'})

hth
Jos

06 May 2009 GRIDXY (v2.2 feb 2008) plot horizontal and vertical grid Author: Jos (10584) Jos (10584)

@Phil, in 3D the lines would become planes. Is this what you want? You can also take a look at REFLINEXYZ to see if that better fills your needs:
http://www.mathworks.com/matlabcentral/fileexchange/16746

29 Apr 2009 MATLAB Programming Example for U2300A USB Modular Data Acquisition (DAQ) module’s Analog Input This program sets the DAQ module to sample immediately without waiting for a trigger signal. Author: Neo Jos (10584)

Why not pack all these examples into a single zip-file?

29 Apr 2009 Rounding Off Number(s) to Specified Significant Figures Round off number(s) to specified significant figures, using two simple/powerful MATLAB functions. Author: Yuzo Toya Jos (10584)

Yes, too simple and its functionality is already abundantly available here of the FEX. However, I should compliment you for the nice help section!

27 Apr 2009 nextprime For any given number (also vpi numbers), find the next prime number in the sequence of primes. Author: John D'Errico Jos (10584)

This is a nice, well written snippet, that prime-addicts will surely appreciate.

09 Apr 2009 Base Converter Converts a number between two different base systems. Author: James Roberts Jos (10584)

The same can be accomplished more efficiently by a minor change to DEC2BASE.

06 Apr 2009 KTHVALUE select the k-th smallest element in a (randomized) list Author: Jos (10584) Jos (10584)

@Luigi: To be honest, I suggest that you use SORT in all cases ... the gain in using KTHVALUE is small. And it is not so much the value of K that determines its running time. This submission is to be regarded as a programming exercise.

06 Apr 2009 UNIQUEWITHEQUALNANS Set unique, treating NaNs as equal (v2.0, mar 2009) Author: Jos (10584) Jos (10584)

@Hoi Wong: apparently TMW changed the internals of UNIQUE, so it is no longer backward compatible. I will update this function shortly.

05 Apr 2009 MATLAB Contest - Data Visualization Starter kit for the 19th MATLAB® Online Programming contest Author: The MATLAB Contest Team Jos (10584)

This submission has no use to anyone but TMW.

24 Mar 2009 CONVOLUTION IN MATLAB WITHOUT USING conv(x,h) enter x=[ ]; % enter in brackets enter h=[ ] Author: imran shezad Jos (10584)

A typical example of a file that has no use for others, written in poor C-style code. Do not bother to download!

12 Mar 2009 Map fields of a structure to output variables The program assigns each field of a structure into output arguments. Author: Hoi Wong Jos (10584)

A superfluous combination of two basic ML commands. Simply using
C = struct2cell(S) ; [a,b,c] = deal(C{:}) ; in your code is clear enough.

10 Mar 2009 CELL2STR CELL2STR converts a cell array of strings into a character array. Author: Daniel Claxton Jos (10584)

Nice programming exercise for duplicating the built-in CHAR function. Please remove.

27 Feb 2009 Multiple assignment The function arg2vars enables to assign more items to a list of variables Author: Miroslav Balda Jos (10584)

Most of this behavior can be handled by DEAL, for instance, a,b,c] = deal([])
(btw, the later example is rather strange as I would expect
"[E,b,c...] = arg2vars" to do the job ...)

The need for putting cell arrays into a cell to assign it to a single variable is in my opinion very contra-intuitive given the fact that a regular array is not split into separate variables.

To me, the only advantage of this submission seems to be that unassigned output arguments are set to empty, but a simple wrapper function around deal will do the same:

function [varargout] = mydeal(varargin) ;

varargout([1 2:nargout]) = {[]} ; % return [] when called without any in/output arguments
if nargin,
    N = max(min(nargin,nargout), 1) ;
    % how many things do we have to be DEALt with?
    [varargout{1:N}] = deal(varargin{1:N}) ;
end

23 Feb 2009 vectorCounter Iteraterates from start vector to finish vector. Author: michaelB brost Jos (10584)

A few suggestions for improvement:
- check that all necessary output arguments are returned. For instance, this function has hardly any use when called with less than 4 output arguments
- make use of the function persistent, so that one can call the function once to initialize it, and then without arguments to update it, without having to go through all the checks and loops again.
- The coding can be improved: instead of the for-loop "% check end >= start" make use of ANY; "hitflag = ~isempty(hitIndex))" instead of the if-statement; etc ...

18 Feb 2009 Convert an amount of seconds to more readable units Convert an amount of seconds to hours/days/weeks/etc. Author: Rody Oldenhuis Jos (10584)

The code has indeed improved and comments have been added, as well as a readable help section (missing, however, a reference to, e.g., DATENUM and ETIME, which also may be used to get the elapsed time in a more readable format).

Still, I doubt it's usefulness and emphasis on details when unneeded. I think 3 stars is a nice rating for this snippet.

btw, are weeks, months, or even days proper scientific units?

18 Feb 2009 Convert an amount of seconds to more readable units Convert an amount of seconds to hours/days/weeks/etc. Author: Rody Oldenhuis Jos (10584)

You missed the eons!

More importantly, you should provide a proper help section, giving details of the input and expected output.The code is also not that nice. Internal comments are almost lacking. MatLab already offers DEBLANK and STRTRIM for trimming white spaces. Moreover, this function is not needed if you had used cell arrays to store the names. The section of "switch plural(amount), case true, ... case false ..., end is much simpler codes as: if amount>1, ... else ... end.

Finally, is it really useful to know the number of seconds remaining when something took millenia, or even days? This focus on unimportant details reminds me of an old russian joke: A man has saved enough money to buy a car in the old Sovjet-Union. The car-dealer tells him that he can pick up his new car two years from now. The man asks: "In the morning or in the afternoon? ". "Why do you care? It is 2 years from now" the car-dealer replies. "Well", the man says, "the plumber is coming in the morning".

14 Feb 2009 findRuns.m Returns indices of n consecutive numbers (you specify the number you want) Author: Richard Heitz Jos (10584)

Before evaluating the algorithm, you can improve the help section and add comments to the code. A proper help section consists of (1) an H1 line, (2) and explanation of the algorithm in plain language with a description of the input and output parameters, (3) and example, and perhaps (4) a See Also line.

As it is, "help finruns" returns nothing really useful, unless one take a closer look at the code.

The approach itself is also the result of translating pseudocode (C?) directly into matlab. Using a logical vector, combined with strfind easily returns the indices: Q = vector == value ; startidx = strfind(Q,[ 0 1]) ; etc ... which has been described many times on the newsgroup.

07 Feb 2009 CIRCULANT (v2.0, feb 2009) (yet another, but fast) Circulent Matrix Author: Jos (10584) Jos (10584)

An update that resolves a bug in forward shifts has been submitted. Please wait for version 2.0.

04 Feb 2009 Linearly spaced multidimensional matrix without loop Generates a N-dimensional matrix of linearly equally spaced points between the matrices d1 and d2. Author: Steeve AMBROISE Jos (10584)

03 Feb 2009 Circulant matrix The circulant matrix generated from a vector as the first row (or first column) Author: John D'Errico Jos (10584)

This well-written function inspired me to submit my own version that uses simple matrix operations which should be up soon. Well done John and thanks for the inspiration.

03 Feb 2009 Linearly spaced multidimensional matrix without loop Generates a N-dimensional matrix of linearly equally spaced points between the matrices d1 and d2. Author: Steeve AMBROISE Jos (10584)

The error indeed seems to be fixed.

02 Feb 2009 Circular Matrix computation Commodity function for building a circulant matrix given a vector Author: Massimo Fierro Jos (10584)

The author should be commended for adding a good help section. Although faster and better algorithms exist, I concur with Husam A. that this is indeed a nice effort, and therefor happily raise my rating.

02 Feb 2009 Linearly spaced multidimensional matrix without loop Generates a N-dimensional matrix of linearly equally spaced points between the matrices d1 and d2. Author: Steeve AMBROISE Jos (10584)

>> linspacendim(0,1)
??? Error using ==> cat
CAT arguments dimensions are not consistent.
Error in ==> LINSPACENDIM at 39
y = cat(NDim+1, Sum1 + Sum2, d2);

This error occurs for every 1-by-N-by-M-... matrix. There are some other issues as well. For instance, in the help:
- LINSPACENDIM or LINSPACEMAT?
- n = 1 in the example?

02 Feb 2009 Circular Matrix computation Commodity function for building a circulant matrix given a vector Author: Massimo Fierro Jos (10584)

Some indexing will give you the same result
V = IN(:) ;
idx = hankel(1:numel(V),0:numel(V)-1)
% idx = hankel(1:numel(V)) + hankel(zeros(1,numl(V)),0:numel(V)-1)
OUT = V(ix) ;

01 Feb 2009 maxmax find the max value and position from a multi-dimensional matrix Author: Simon Jos (10584)

The solution suggested by S.A. is (almost) as poor as the original submission ...

31 Jan 2009 Circular Matrix computation Commodity function for building a circulant matrix given a vector Author: Massimo Fierro Jos (10584)

Please add a proper help section including a H1 line, syntax and example. At present this submission is completely useless for anyone but the author.

30 Jan 2009 Seconds to hours, minutes, seconds Converts a time in seconds to a time in hours, minutes and seconds. Author: Simon Robinson Jos (10584)

should read: _NO_ H1 line ...

30 Jan 2009 Seconds to hours, minutes, seconds Converts a time in seconds to a time in hours, minutes and seconds. Author: Simon Robinson Jos (10584)

This file contains H1 line , no help section and no comments, which makes it rather useless. First, how will you find the name of this file in the future if you cannot use LOOKFOR. Secondly, even if you find it, how will you know how to use it, if you cannot use HELP. Third, the total lack of comments makes it difficult to decipher what an end-user should do. In other words, it is only useful to someone with some matlab skills already. On the other hand, she or he will probably stick to something more matlab-ish like:

TimeInSecs = 7261 ;
disp(datestr(datenum(0,0,0,0,0,TimeInSecs),'HH:MM:SS'))

 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com