Problem 1755. Fix the last element of a cell array
Note: this is lifted directly from Puzzler for a Monday (on MATLAB Answers) by the cyclist.
----
Given a cell array of strings
A = {'MATLAB','HURRAY','SPARKLY','KITTENS','FUN'};and a particular string value
B = 'KITTENS';
ensure that B is the last element of the cell array. If it isn't, move it to the end of A.
You cannot assume that B appears at all (in which case return A unchanged), but you can assume B does not appear more than once.
So in the example,
C = {'MATLAB','HURRAY','SPARKLY','FUN','KITTENS'};
Solution Stats
Problem Comments
-
1 Comment
Solutions to light problems like this enable scaling up ideas to heavy, complex algorithms.
Solution Comments
Show commentsProblem Recent Solvers1682
Suggested Problems
-
Find relatively common elements in matrix rows
2141 Solvers
-
Replace NaNs with the number that appears to its left in the row.
3050 Solvers
-
Duplicate each element of a vector.
631 Solvers
-
We love vectorized solutions. Problem 1 : remove the row average.
872 Solvers
-
Is this triangle right-angled?
6281 Solvers
More from this Author50
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!