Cody

Problem 1741. Numeric array to cell array of strings (easy)

Solution 287879

Submitted on 23 Jul 2013 by Evan
  • Size: 11
  • This is the leading solution.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
%% A = [1 2 3 2 3 1 3 1 2]; C = {'yes','no','maybe'}; B_correct = {'yes' 'no' 'maybe' 'no' 'maybe' 'yes' 'maybe' 'yes' 'no'}; assert(isequal(ind2str(A,C),B_correct))

ans = 'yes' 'no' 'maybe' 'no' 'maybe' 'yes' 'maybe' 'yes' 'no'

2   Pass
%% A = ones(20,1); C = {'apples','oranges'}; assert(all(strcmp(ind2str(A,C),'apples')))

ans = Columns 1 through 8 'apples' 'apples' 'apples' 'apples' 'apples' 'apples' 'apples' 'apples' Columns 9 through 16 'apples' 'apples' 'apples' 'apples' 'apples' 'apples' 'apples' 'apples' Columns 17 through 20 'apples' 'apples' 'apples' 'apples'

3   Pass
%% A = randi(1000,[22,10]); C = arrayfun(@(x) num2str(x),1:1000,'uni',0); assert(isequal(A,cellfun(@(c) str2num(c),ind2str(A,C))))

ans = '535' '294' '776' '695' '351' '616' '949' '256' '686' '230' '376' '874' '734' '923' '700' '374' '59' '413' '108' '799' '383' '824' '146' '656' '489' '439' '24' '704' '447' '761' '218' '999' '616' '634' '821' '600' '209' '748' '128' '341' '846' '375' '799' '402' '330' '878' '295' '38' '520' '232' '72' '509' '821' '358' '95' '116' '367' '782' '257' '709' '363' '749' '369' '454' '832' '986' '851' '317' '160' '407' '110' '400' '810' '839' '427' '858' '942' '415' '422' '945' '914' '932' '61' '391' '257' '442' '64' '88' '559' '654' '41' '428' '91' '890' '606' '832' '410' '350' '301' '104' '228' '43' '806' '812' '578' '293' '948' '130' '736' '781' '352' '158' '547' '823' '962' '511' '223' '305' '341' '698' '891' '362' '896' '865' '64' '752' '48' '947' '45' '108' '484' '59' '187' '339' '720' '381' '8' '771' '409' '207' '210' '844' '833' '64' '903' '267' '220' '822' '323' '934' '319' '890' '63' '267' '196' '263' '674' '115' '359' '205' '900' '59' '601' '93' '898' '583' '132' '204' '55' '41' '7' '203' '61' '672' '822' '444' '705' '593' '749' '543' '174' '88' '234' '945' '666' '447' '385' '11' '80' '5' '768' '574' '220' '657' '657' '979' '363' '272' '293' '576' '176' '437' '260' '417' '536' '792' '963' '661' '417' '447' '508' '89' '600' '242' '149' '212' '88' '201' '700' '847'

4   Pass
%% A = randi(2,[1,100]); C = {'0','1'}; assert(isequal(A-1,cellfun(@(c) str2num(c),ind2str(A,C))))

ans = Columns 1 through 14 '1' '0' '1' '0' '1' '0' '1' '1' '1' '0' '1' '0' '1' '0' Columns 15 through 28 '1' '0' '1' '1' '0' '0' '0' '0' '0' '0' '0' '0' '1' '1' Columns 29 through 42 '1' '1' '1' '1' '0' '0' '0' '0' '1' '0' '0' '1' '1' '1' Columns 43 through 56 '1' '1' '1' '0' '0' '1' '1' '1' '1' '1' '0' '1' '0' '1' Columns 57 through 70 '0' '1' '1' '0' '0' '1' '0' '0' '0' '1' '1' '1' '0' '1' Columns 71 through 84 '0' '0' '0' '1' '1' '1' '0' '1' '0' '1' '1' '0' '0' '1' Columns 85 through 98 '0' '0' '0' '0' '0' '1' '0' '1' '1' '0' '1' '0' '0' '1' Columns 99 through 100 '1' '0'

5   Pass
%% A = [2 4 4 2 2 4]; C = {'foo','bar','baz','qux'}; B_correct = {'bar' 'qux' 'qux' 'bar' 'bar' 'qux'}; assert(isequal(ind2str(A,C),B_correct))

ans = 'bar' 'qux' 'qux' 'bar' 'bar' 'qux'

Suggested Problems

More from this Author44

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!