Vector/matrix Initialization

Matlab newbie here, so be gentle.
I am trying to port some Matlab code to C/C++ and am struggling to understand some of the syntax.
In particular, the following code segment eludes me:
if i0<i1
k=[i1 i i0];
elseif i0>i
k=[i0 i1 i];
elseif i1<i
k=[i i0 i1];
end
v=1:length(s);
v(k(1))=[];
v(k(2))=[];
v(k(3))=[];
for k=1:length(v)
px=s(v(k),1);
py=s(v(k),2);
end;
As I understand it, the first if-then block creates k as a one row, three column matrix. I am simply lost by the next code block. Any help in understanding what it means (especially in C/C++ form) appreciated.
Thanks

 Risposta accettata

Stephen23
Stephen23 il 2 Feb 2017
Modificato: Stephen23 il 2 Feb 2017
The first two blocks don't make much sense. Assuming that the one of those if conditions is true then vector k will get defined with the same three value (in different orders). The vector k is then used to remove some values from v, but the order is irrelevant. k is not used again (it gets reassigned in the third block of code). Therefore the first two blocks are simply equivalent to:
v = 1:length(s);
v([i1,i,i0]) = [];

9 Commenti

And note that Stephen's "assuming that the one of those if conditions is true" is not idle words. There are combinations of i, i0, i1 (e.g. i == i0 == i1) for which none of the if will be true, resulting in k not being created and the next section erroring due to the undefined k variable.
The last block also does not make any sense, since it just overwrite the same variables over and over without using them. The end result is just:
px = s(v(end), 1);
py = s(v(end), 2);
And from that piece of code, it's also clear that s is a matrix. Therefore the use of length(s) in the 2nd block is also problematic since it may return the number of rows or columns (whichever is greater) but v is use to index rows exclusively. The author should have use size(s,1) instead, to make sure he always had the number of rows.
With cases like this, you may be better off trying to understand what the underlying algorithm was and coding it yourself rather than porting iffy code.
Thanks for these responses. To be clear, the expression: "v(k(1))={}" is simply setting that element to zero (as opposed to deleting the element). Is that correct?
Good catch on the if-then sequence. However, earlier code forces i9, i, and i1 to be different. So, at least one of the conditions will be executed.
Stephen23
Stephen23 il 2 Feb 2017
Modificato: Stephen23 il 2 Feb 2017
@James Pistorino: the code v(k(1))=[] will remove (delete) an element of the array v. There are no zeros shown in that code. My answer said "remove some values", and not "change to zero". The syntax is explained here:
If it changed the value to zero, then it would be v(k(1)) = 0;
OK. Thanks. That is what I was looking for. It looks like the code is trying to order the position references so that when it deletes one from the vector v, it will not change the other references. Thus, it deletes elements from the back of the vector forward. Thanks again.
Stephen23
Stephen23 il 2 Feb 2017
Modificato: Stephen23 il 2 Feb 2017
@James Pistorino: your interpretation seems reasonable, but I hope you can see that it is an awful misuse of MATLAB. Some programmer was thinking about arrays and pointers and loops and iterating... what is the point of using a high level language if it is written like C ?
Well, at least it might give you some hints about how to do it in your ported version.
Sorry, one more question. What does the line "v=1:length(s)" do? I am guessing that it creates a vector (v) and pre-loads it with values from 1 to the length of (s). Correct?
Thanks
Guillaume
Guillaume il 2 Feb 2017
Yes, but as stated most likely the author misused length which returns the number of elements in the largest dimension of s (which is obviously 2d). So depending on the size of s, v will be 1 to the number of columns, or 1 to the number of rows. Then v is used to index rows only, so again a bug waiting to happen.
Stephen23
Stephen23 il 2 Feb 2017
Modificato: Stephen23 il 2 Feb 2017
@James Pistorino: to learn the basics of MATLAB you should work through these tutorials (they will not take long if you have experience with other languages, but will introduce you to some extremely fundamental concepts, especially code vectorization):
And you might like to read this too:
PS: when you write your own MATLAB code avoid using length: use size or numel.
Great. Thanks again.
I see what you are saying about length(s). While it is bad form to do it the way he did, other code guarantees that s can never have a length less than 3 and it always will have two columns. Thus, he should not have an error.
This is code from a computer science research paper. So the author may be somewhat newer to Matlab coding.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by