Doubt with algorithm and his results

Sorry, all problem statement is wrong, and I do not know how to delete these questions, because I do not want to confuse people who may read it.

2 Comments

It is correct the logic of the algo?
Thank you very much
Stephen23
Stephen23 on 11 Sep 2015
Edited: Stephen23 on 11 Sep 2015
@Esteban Fernandez: please never delete your question like this. This is a community forum, and the answers are only useful when you leave your question text intact. We are all volunteers and we write answers that everyone can read and possibly find useful. We are not your personal tutors or code fixing service. We are not here to offer you and only you this answer, it was intended to be for all users who might find the topic interesting or have similar problems. When you selfishly delete the question than you have abused the purpose of this forum and our own time and effort.

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 8 Sep 2015
Edited: Stephen23 on 8 Sep 2015
Your code is much more complicated than it needs to be: in particular multiple nested loops are not good practice, the indexing could be much more helpful, many simple logical operations should be vectorized, and you don't use any functions to help you (e.g. diff, any). Here is how I implemented the first three steps, you can easily figure out the last two steps.
M = [2;3;0;1;2;0;1;0;1;4];
% 1
idx = 2==M;
for k = find(idx')
% 1
idx(k) = all(3~=M(min(end,k+(1:3))));
% 2
idx(k) = idx(k) && any(0<=diff(M(k+3:end)));
% 3
idx(k) = idx(k) && any(1<=diff(M)); % not clearly defined!
end
This creates the following vector, which is true for the single 2 which passes the first three steps:
idx =
0
0
0
0
1
0
0
0
0
0
Actually I stopped at the third step because it is not clear what is required here: it seems that step 2 is a subset of step 3, so you only need to test step 3. If this is not the case then you need to specify these steps much more clearly.

2 Comments

Thank you very much!!
i am going to study your code and i am going to try to have the last steps.
Thank you very much!!
My Pleasure. I am happy to help if you have any questions about how it works (please add comments to this answer). You should read about array indexing and code vectorization in MATLAB, which will help you to understand that code.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!