Problem

MATLAB

Consider the following nested cell array,

q = {{'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h'}}

Write a for-loop that extracts all the letters in the list and finally prints them all as a single string,

abcdefgh
Python

Consider the following nested list,

q = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]

Write a for-loop that extracts all the letters in the list and finally prints them all as a single string,

abcdefgh

Solution

MATLAB
q = {{'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h'}};
concatenatedString = '';
for cellElement = q
    for nestedCellElement = cellElement{1}
        concatenatedString = [ concatenatedString , nestedCellElement{1} ];
    end
end
disp(concatenatedString)
abcdefgh
Python
q = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]
s = ''
for i in q:
    for j in range(len(i)):
        s = s + i[j]
print(s)
abcdefgh

Comments