Search This Blog

Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts

Thursday, February 21, 2013

append a number to a string in matlab

Problem: Given a prefix say 'foo', generate a cell array of strings in Matlab such that each string has a number appended to it.

Ans:
>> n=5; cas = cellstr([repmat('foo', n, 1), num2str([1:n]')])

cas = 

    'foo1'
    'foo2'
    'foo3'
    'foo4'
    'foo5'
 Spaces are automatically appended when the appended numbers have wider range.
>> n=10; cas = cellstr([repmat('foo', n, 1), num2str([1:n]')])

cas = 

    'foo 1'
    'foo 2'
    'foo 3'
    'foo 4'
    'foo 5'
    'foo 6'
    'foo 7'
    'foo 8'
    'foo 9'
    'foo10'
Adding another string at the top of the array is easy
>> n=10; cas = [{'raju'}; cellstr([repmat('foo', n, 1), num2str([1:n]')])]

cas = 

    'raju'
    'foo 1'
    'foo 2'
    'foo 3'
    'foo 4'
    'foo 5'
    'foo 6'
    'foo 7'
    'foo 8'
    'foo 9'
    'foo10'
which can also be done by
>> n=10; cas = cellstr([repmat('foo', n, 1), num2str([1:n]')]); cas2 = [{'raju'}; cas]

cas2 = 

    'raju'
    'foo 1'
    'foo 2'
    'foo 3'
    'foo 4'
    'foo 5'
    'foo 6'
    'foo 7'
    'foo 8'
    'foo 9'
    'foo10'

Sunday, March 11, 2012

matlab to excel

Q) what is the excel equivalent of matlab's ismember function
A) vlookup

The syntax is vlookup(lookup_value, table_array, col_index_num, [range_look_up])

It can be used to check if a value exists in a column, compare two columns etc.,

The col_index_num starts from 1.
I usually give the 4th argument as false, so I can get an exact match. See help in excel for more details.

ISNA() - similar to matlab's isnan function, is another function that comes in handy with vlookup. It tells whether a number is #N/A or not.

Followers