Problem 41. Cell joiner
Solution Stats
Problem Comments
-
12 Comments
There has been a function added in 2013a for this!!
I make it in R2013a,but I don't know the R2012b or other previous versions have this function.
Most of the solutions consider that delim=1char
i don't know
tricky one!!
nicequestion
Hello everyone :)
Just a quick one: the code I provide below here work fine in Matlab (r2019b), so each solution matches the one I'm asked to provide. However, when evaluating the code on Cody, I get "Assertion failed".
Can anyone please explain me if it's a matter of conversion or some details I am missing?
KR, Andrea
out_str=[];
for jj=1:length(in_cell)
tmp=char(in_cell(jj));
c=[tmp,delim];
if jj==1
out_str=c;
else
out_str=[out_str,c];
end
end
I used strjoin(in_string,' ')
passed the first two, third one failed.
no way for me
nice biology concept
This one is kinda hilarious, as (hint here) it may punish you for defining TOO much...
cool problem!
Solution Comments
-
1 Comment
I mean these problems are kind of neat because they showcase which functions exist in matlab but then on the other hand these functions just exist in matlab so there's not really the need to "write" a function...
-
1 Comment
Remember, answer needs to be a string...
-
1 Comment
nice
-
1 Comment
join doesn't work here?
-
1 Comment
Super easy one
-
1 Comment
thanks!
-
1 Comment
This would be the replication of a single built-in function.
-
1 Comment
funny!
-
2 Comments
Assertion fails in Cody even though the assertion passes in MATLAB R2014b. Does any one know why?
Hi. The assertion could only have passed outside of Cody if the wrong "correct solution" were specified for comparison, and this would happen if you copy the HTML-rendered text — due to a kind of bug in the way the solution vector is displayed on the Cody webpage (better: copy from the HTML _source code_). -----
The correct solution has delimiters (here shown as ^ for clarity) as in 'this^one^^has^ ^some tricky^stuff', whereas your code produces 'this^one^has^some tricky^stuff', so you don't get the correct result between "one" and "has", nor between "has" and "some".
-
3 Comments
I think that the test 2 must be checked.
I compared the test 2 correct answer with the program output using strcmp, and it gave 1 as output.( This was before introducing the program lines which also take into consideration the single space given in 5th element of in_cell in test 2)
Anyone, who has solved or has an idea how to solve, Please let me know what is the error in my program.
Thanks, By the way, program might be a bit lengthy.
THE TEST CASE 2 OUTPUT YOU HAVE DISPLAYED IS WRONG
CORRECT IT.
There are two issues here:
(1) The HTML display in Cody automatically only displays a single space. Therefore, while the solution *vector* _specified_ as the answer to Test 2 in Cody is correct, the *text* that is _displayed_ is not. You can confirm that the correct information was specified by viewing the HTML source. It is unfortunate that this is not displayed correctly. Of course, any results should match the solution, not the incorrect display.
(2) Your candidate solution fails because it is incorrect. Even if it matches the HTML-parsed display, it doesn't match the true specified solution vector. To make this apparent, imagine if delim='^' (no other changes). The correct solution to Test 2 would then be 'this^one^^has^ ^some tricky^stuff', whereas your output would be 'this^one^has^ ^some tricky^stuff' (provided in Solution 1280078). In other words, you needed a double space between "one" and "has".
-
1 Comment
My solution is:
function out_str = cellstr_joiner(in_cell, delim)
y=in_cell;
y(find(cellfun(@isempty,cellfun(@strtrim,y,'UniformOutput',false))))=[];
out_str = strjoin(y);
out_str(find(out_str==' '))=delim;
end
On Matlab all tests pass but on the cody website the second test fail!
-
2 Comments
This code is working as expected in MATLAB command window. can i know why it is failed here
why deblank?
-
2 Comments
ok, is not really elegant not short, but it's correct. why doesn't it pass the test?
The problem is that your output is a cell string and not a character array. You can get a char array by adding out_str=char(out_str); in the end, or work with char arrays from the beginning by using in_cell{1} and in_cell{i} instead of in_cell(1) and in_cell(i).
-
1 Comment
I think there is something wrong here, this solution seems to work.
-
2 Comments
nice, I had never heard of this one!
I had tried this, but kept getting: "strjoin not found" on my workstation :(
So I had to work around it.
-
1 Comment
The best one! (without STRJOIN)
-
2 Comments
answers just the test suite...
There are better ways to cheat!
-
1 Comment
Simple and easy to understand, and works in versions before strjoin appeared.
-
1 Comment
Ugh, for loop!
-
1 Comment
printf works wonders
-
1 Comment
Wow! Brilliant!
Problem Recent Solvers4791
Suggested Problems
-
Select every other element of a vector
28201 Solvers
-
Sort a list of complex numbers based on far they are from the origin.
5119 Solvers
-
2309 Solvers
-
578 Solvers
-
Matlab Basics - Convert a row vector to a column vector
564 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!