Interview qustion matlab function
显示 更早的评论
I asked to implement function it's input is a vector and plot it with time.
ex: lets suppose the input vector is [ 0 1 2 3 3 3 4 4 4 4 4 5 5 5 6 7 8]
if you can see there are 17 element so the time vector will be from [1......17]
but back to the input vector and take these values [0 1 2 3] can be represent from [1 3]
but time must be still [1 2 3 4], so when the slop is equal i need to keep first and last one and delete the middle elements.
back to input vector again i will see this [3 3 3] can be represent from [3 3]
but time must be still [3 4 5], so here will be constant line.
and so one the next input [4 4 4 4 4] equal to [4 4] with time from [6 7 8 9 10], Linear constant
so you need to generate new vector but the shape of it equal to the original shape.
Thanks in advance.
7 个评论
Image Analyst
2018-6-29
Is this a translation from a different language? If so, I can understand why it doesn't make sense. Otherwise, I would not work for that company. If every task they give you is explained and laid out like that, you'd have a very rough time working there, because that's essentially gibberish.
Guillaume
2018-6-29
In any case, what is the difficulty? It only takes two diff and a find to generate the shortened vector and corresponding time.
David Goodmanson
2018-6-29
suppose a = [1 2 2 2 2 3 4 5 6 7 7 7 7 8]. How does one accomplish the task with two diff and a find?
Rik
2018-6-29
One call to unique and an index then. The point raised by Image Analist is still valid and one I would agree with.
Guillaume
2018-6-29
a = [1 2 2 2 2 3 4 5 6 7 7 7 7 8]
idx = [1, find(diff(diff(a)))+1, numel(a)]
plot(idx, a(idx));
David Goodmanson
2018-6-29
yes, I forgot that (in this case) points 3 thru 6 are not included.
回答(1 个)
Mohammed kandeel
2018-7-9
0 个投票
3 个评论
Image Analyst
2018-7-9
It's not how many years of experience he has, it's that he's a freaking mind reader. I have 13 years of experience in MATLAB and I still can't figure it out. I read it many many times and I have no idea. The only ideas I had was that it was either (1) a bad original problem statement, (2) a bad Google translation, or (3) a bad translation on your part.
For example
"take these values [0 1 2 3] can be represent from [1 3]"
Huh? How/why on earth can/should an array of four values be represented by only two values, the second one and the fourth one? Stumps me.
And then it says
"but time must be still [1 2 3 4], so when the slop is equal i need to keep first and last one and delete the middle elements."
Again, huh? Does that mean that you now have [1,4]? OK, but why keep the 1st and 4th element this time when it was the 2nd and 4th for the [0,1,2,3] case? I have no idea.
Guillaume
2018-7-9
Yes, the hardest part is working out what the question is! Once that is understood, the problem reduces to finding the changes of slope in the data which is trivial to do in matlab. It doesn't take years of experience to understand that diff(X) is the slope, and diff(diff(X)) is the change of slope (2nd derivative).
Mohammed kandeel
2018-7-14
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!