tough but good.
I used a cell array and the following info:
https://uk.mathworks.com/matlabcentral/answers/361968-getting-first-element-of-each-cell-array-with-different-sizes
Test | Status | Code Input and Output |
---|---|---|
1 | Pass |
assessFunctionAbsence({'regexp','regexpi','regexprep','str2num'},'FileName','locOf5.m')
|
2 | Pass |
x = 2:2:20;
y_correct = 0;
assert(isequal(locOf5(x),y_correct))
c =
0×0 empty cell array
c =
1×1 cell array
{0×0 double}
c =
1×2 cell array
{0×0 double} {0×0 double}
c =
1×3 cell array
{0×0 double} {0×0 double} {0×0 double}
c =
1×4 cell array
{0×0 double} {0×0 double} {0×0 double} {0×0 double}
c =
1×5 cell array
{0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
c =
1×6 cell array
{0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
c =
1×7 cell array
{0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
c =
1×8 cell array
{0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
c =
1×9 cell array
{0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
c =
1×10 cell array
{0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
idx =
1×10 logical array
0 0 0 0 0 0 0 0 0 0
out =
0 0 0 0 0 0 0 0 0 0
out =
0 0 0 0 0 0 0 0 0 0
out =
0
|
3 | Pass |
x = rot90(1:10);
y_correct = 6;
assert(isequal(locOf5(x),y_correct))
c =
0×0 empty cell array
c =
1×1 cell array
{[6]}
idx =
logical
1
out =
0
out =
6
|
4 | Pass |
x = [1 2 5
5 9 1
5 6 5];
y_correct = [2 0 1];
assert(isequal(locOf5(x),y_correct))
c =
0×0 empty cell array
c =
1×1 cell array
{2×1 double}
c =
1×2 cell array
{2×1 double} {0×1 double}
c =
1×3 cell array
{2×1 double} {0×1 double} {2×1 double}
idx =
1×3 logical array
1 0 1
out =
0 0 0
out =
2 0 1
|
5 | Pass |
x = magic(5);
y_correct = [0 2 0 0 0];
assert(isequal(locOf5(x),y_correct))
c =
0×0 empty cell array
c =
1×1 cell array
{0×1 double}
c =
1×2 cell array
{0×1 double} {[2]}
c =
1×3 cell array
{0×1 double} {[2]} {0×1 double}
c =
1×4 cell array
{0×1 double} {[2]} {0×1 double} {0×1 double}
c =
1×5 cell array
{0×1 double} {[2]} {0×1 double} {0×1 double} {0×1 double}
idx =
1×5 logical array
0 1 0 0 0
out =
0 0 0 0 0
out =
0 2 0 0 0
|
6 | Pass |
x = [1 2 3 4 5;
5 4 3 2 1
2 3 5 2 1
1 5 2 6 8
3 5 2 2 5];
y_correct = [2 4 3 0 1];
assert(isequal(locOf5(x),y_correct))
% %%
% x = randi([-10,10],20,1e6);
% x(x==5) = 0;
% p = sort(randi([0 size(x,1)],5,size(x,2)));
% y_correct = p(1,:);
% p(2:end,~y_correct) = 0;
% [~,col,v] = find(p);
% x((col-1)*size(x,1)+v) = 5;
% assert(isequal(locOf5(x),y_correct))
c =
0×0 empty cell array
c =
1×1 cell array
{[2]}
c =
1×2 cell array
{[2]} {2×1 double}
c =
1×3 cell array
{[2]} {2×1 double} {[3]}
c =
1×4 cell array
{[2]} {2×1 double} {[3]} {0×1 double}
c =
1×5 cell array
{[2]} {2×1 double} {[3]} {0×1 double} {2×1 double}
idx =
1×5 logical array
1 1 1 0 1
out =
0 0 0 0 0
out =
2 4 3 0 1
|
Extract leading non-zero digit
1205 Solvers
Find the largest value in the 3D matrix
1056 Solvers
376 Solvers
299 Solvers
669 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!