Thursday, July 30, 2009

Matlab "strcnt" problem?

======Write a matlab subfunction (strcnt) that will count the number of times the ascii characters appear in the given string array. The output arguments are composed of two vectors. The first vector containing the list of symbols while the second vector contains the number of times the symbol appears. Test your subfunction by loading the file mydata1. This file contains the string array stored in variable s. Store the first vector in variable L while the second vector in variable C.=====





help needed, please post the syntax hereat yahoo answers

Matlab "strcnt" problem?
Try making an M file with the following lines:





function [cellarray, L, C] = strcnt(x)


% character frequency analysis of string x


warning off;


n = 1;


a (1) = x(1);


c (1) = 0;





for i = 1 : length (x);


found = 0;


for k = 1 : n;


if a (k) == x (i);


c (k) = c (k) + 1;


found = 1;


end;


end;





if found == 0;


n = n + 1;


a (n) = x(i);


c (n) = 1;


end;


end;





cellarray = cell (length (c) , 2);





for i = 1 : n;


cellarray (i, 2) = cellstr (c (i));


cellarray (i, 1) = num2cell (a (i));


end;





L = c;


C = a;


warning on;





%-------------------------------------...





Save the M-file in your Matlab working directory.





To test your function, try:





s = ‘Your text goes here. Try to use a variety of characters: a, b, c, …, x, y, z, …’;


[A, L, C] = strcnt (s)





Enjoy it.


No comments:

Post a Comment