Cody
Follow


Problem 1899. Convert a Cell Array into an Array

Mayla on 19 Sep 2023
Latest activity Reply by Adam Danz on 19 Sep 2023

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,' ' )
Adam Danz
Adam Danz on 19 Sep 2023
I didn't want to give away a cody solution ;)
Dyuman Joshi
Dyuman Joshi on 19 Sep 2023
@Adam Danz - Why did you delete your answer?
Matt J
Matt J on 19 Sep 2023
x = {'01', '56'; '234', '789'};
y=[x{:}]
y = '0123456789'
x={'' 'a' '1'; 'AA' 'BB' 'CC'; 'dog' 'cat' 'car'};
y=[x{:}]
y = 'AAdogaBBcat1CCcar'
x={'We' 'do' ;'ll ' 'ne.'};
y=[x{:}]
y = 'Well done.'
Adam Danz
Adam Danz on 19 Sep 2023
Ahhh I see. Thanks @Dyuman Joshi!
Dyuman Joshi
Dyuman Joshi on 19 Sep 2023
@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.';
Adam Danz
Adam Danz on 19 Sep 2023
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?