Thats the task:
Given a square cell array:
x = {'01', '56'; '234', '789'};
return a single character array:
y = '0123456789'
I wrote a code that passes Test 1 and 2 and one that passes Test 3 but I'm searching a condition so that the code for Test 3 runs when the cell array only contains letters and the one for Test 1 and 2 in every other case. Can somebody help me?
This is my code:
y = []
[a,b]=size(x)
%%TEST 3
delimiter=zeros(1,a)
delimiter(end)=1
delimiter=repmat(delimiter,1,b)
delimiter(end)=''
delimiter=string(delimiter)
y=[]
for i=1:a*b
y = string([y x(i)])
end
y=join(y,delimiter)
y=erase(y,'0')
y=regexprep(y,'1',' ')
%%TEST 1+2
for i=1:a*b
y = string([y x(i)])
y=join(y)
end
y=erase(y,' ' )
6 Comments
I didn't want to give away a cody solution ;)
x = {'01', '56'; '234', '789'};
y=[x{:}]
x={'' 'a' '1'; 'AA' 'BB' 'CC'; 'dog' 'cat' 'car'};
y=[x{:}]
x={'We' 'do' ;'ll ' 'ne.'};
y=[x{:}]
@Adam Danz, OP is talking about Cody Problem 1899 - Convert a Cell Array into an Array. Here are the test cases for that problem, inputs and expected outputs -
So, Your answer works.
%%
x={'01', '56'; '234', '789'};
y_correct = '0123456789';
%%
x={'' 'a' '1'; 'AA' 'BB' 'CC'; 'dog' 'cat' 'car'};
y_correct='AAdogaBBcat1CCcar';
%%
x={'We' 'do' ;'ll ' 'ne.'};
y_correct='Well done.';
I may have answered your question too quickly without understanding the second part to your question. Could you provide examples of when the cell array only contains letters and any other possibilities for x?