Vector/matrix Initialization

35 ビュー (過去 30 日間)
James Pistorino
James Pistorino 2017 年 2 月 2 日
編集済み: Stephen23 2017 年 2 月 2 日
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

採用された回答

Stephen23
Stephen23 2017 年 2 月 2 日
編集済み: Stephen23 2017 年 2 月 2 日
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 件のコメント
Stephen23
Stephen23 2017 年 2 月 2 日
編集済み: Stephen23 2017 年 2 月 2 日
@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.
James Pistorino
James Pistorino 2017 年 2 月 2 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by