issues extracting sub matrix

matrix x=[1:10]. I need to extract the submatrix s=[6, 4, 2] from x. every time i try to use the command:
x=[1:10]
s=[(min(x)+5:2:max(x)-6)]
i get this error, when the solution should be s= 6, 4:*
s =
1×0 empty double row vector
any help as to why this function will not extract a submatrix would be appreciated!

5 件のコメント

Jan
Jan 2017 年 1 月 25 日
編集済み: Jan 2017 年 1 月 25 日
@B: Do you see, that the editor underlines the square brackets and shows a warning? [] is the operator for horizontal concatenation. 1:10 is a vector already, therefore you do not have to concatenate it again.
Guillaume
Guillaume 2017 年 1 月 25 日
The square brackets are not need for s either. It's all unnecessary clutter that slows the code down (square brackets are translated to a call to horzcat) and make it more difficult to read.
John BG
John BG 2017 年 1 月 26 日
編集済み: John Kelly 2017 年 2 月 3 日
feel free to use the brackets the best you you consider.
MATLAB allows the omission of brackets when defining ranges in the way you have, yet it helps readers, it's ok, syntactically correct, it's ok there is more than one way of defining ranges.
Guillaume
Guillaume 2017 年 1 月 26 日
編集済み: Guillaume 2017 年 1 月 26 日
Well, if you want extra useless character why not
x = [[[((((1)'*(((1)'')''+(0)')'')'':-(-1):10^1))]]]
It produces the same result as
x = 1:10
Matlab allows the addition (there's no omission, no bracket is the default state) of plenty of characters that may or may not do anything depending on context. Useless clutter does not make the code more readable.
Jan
Jan 2017 年 1 月 26 日
編集済み: John Kelly 2017 年 2 月 3 日
It seems like this operator for concatenation is confused with a constructor of vectors, but this is a misunderstanding. It is not just my opinion or a question of taste, but even Matlab's code analyser suggests to omit this clutter by showing an MLint warning in the editor. This might not convince you, but it convinces me. And therefore you are correct: I will keep telling users of this forum how to improve Matlab code.
See Answers: why-not-use-square-brackets for more details and an example of speeding up indexing by a factor of 4 by avoiding the brackets around an indexing vector.

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

 採用された回答

Guillaume
Guillaume 2017 年 1 月 25 日
編集済み: Guillaume 2017 年 1 月 25 日

2 投票

The only safe way to make your expression work for any arbitrary x where min(x)+5 may or may not be greater than max(x)-6 is to make the sign of the increment dependent on the sign of the difference between the bounds. Therefore:
x = 1:10; %brackets not needed!
startbound = min(x)+5;
endbound = max(x)-6;
if endbound > startbound
s = startbound:2:endbound;
else
s = startbound:-2:endbound;
end

その他の回答 (2 件)

John BG
John BG 2017 年 1 月 23 日

0 投票

Because
(min(x)+5<max(x)-6)
=
0
With a positive step, unless you provide the initial element of the range smaller than the end element MATLAB will not attempt to guess it.
s=[max(x)-6:2:min(x)+5]
=
4 6
or you provide a negative step
s=[(min(x)+5:-2:max(x)-6)]
s =
6 4
.
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
Jan
Jan 2017 年 1 月 26 日
編集済み: Jan 2017 年 1 月 26 日

0 投票

Another solution:
x = 1:10;
a = min(x)+5;
b = max(x)-6;
s = x(a:(2 * sign(b-a)):b); % Fails for a==b, thanks Guillaume
w = 2 * (-1 + 2 * (b > a)); % Not cute
s = x(a:w:b);

1 件のコメント

Guillaume
Guillaume 2017 年 1 月 26 日
Works as long, as a ~= b. Otherwise you get an increment of zero and thus an empty vector.

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

B
B
2017 年 1 月 23 日

編集済み:

2017 年 2 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by