Matrix indexing - Picking columns

So I trying to give an output a matrix which contains every fifth column of input (A). Below is what my current script look like.
function B = Script30( A )
B = A(:,1:5:end);
end

5 件のコメント

Birdman
Birdman 2018 年 4 月 15 日
So, question?
bondpen
bondpen 2018 年 4 月 15 日
So when i input a matrix it will output only every fifth columns of input matrix.
Guillaume
Guillaume 2018 年 4 月 15 日
Important difference: your code is not a script but a function. It's begging for confusion to name a function script***.
Your function already returns every fifth column of the matrix, starting at column 1, so it's not clear what problem you're having
bondpen
bondpen 2018 年 4 月 15 日
編集済み: bondpen 2018 年 4 月 15 日
I just in habit in this naming scheme lmao.
It giving me rows instead of columns.
Jan
Jan 2018 年 4 月 16 日
What give you rows? The shown code extracts columns.

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

 採用された回答

David Fletcher
David Fletcher 2018 年 4 月 15 日

1 投票

Do you mean this instead of your 1:5:end?
5:5:end
If you explicitly also want column 1 the you could use [1 5:5:end]

12 件のコメント

bondpen
bondpen 2018 年 4 月 15 日
would like it output every 5th columns of the input. Also you second shouldn't it be something like [1,5:5:end]?
David Fletcher
David Fletcher 2018 年 4 月 15 日
Maybe an example of exactly what you do want is required, because I have no idea
bondpen
bondpen 2018 年 4 月 15 日
For example A is Matrix which is guaranteed to even multiple of 5 to ensure the output will have column of 5 and if it more than 5 it should give the 5th column and 10th column and vice versa.
Jan
Jan 2018 年 4 月 15 日
@David Wu: The question is still not clear. What's wrong with David Fletcher's suggestion? It seems to do what you are asking for.
Guillaume
Guillaume 2018 年 4 月 15 日
@david,
[1 5:5:end] and [1, 5:5:end] are exactly the same. In matlab, you can use a space or a comma for separating horizontal elements of a vector. Using a comma is considered safer.
bondpen
bondpen 2018 年 4 月 15 日
This works greats but the output is rows instead the desire columns.
David Fletcher
David Fletcher 2018 年 4 月 15 日
編集済み: David Fletcher 2018 年 4 月 15 日
@Guillaume I don't dispute it. Unfortunately that's not what you've got. 1:5:end ~= 5:5:end ~= [1 5:5:end]
bondpen
bondpen 2018 年 4 月 15 日
編集済み: bondpen 2018 年 4 月 15 日
I have replaced the code to below: It still output rows, how did you make it out columns instead?
function B = Script30( A )
B = A(5:5:end)
end
David Fletcher
David Fletcher 2018 年 4 月 15 日
A(:,5:5:end);
Gives me a columnised output on my system with a test matrix
David Fletcher
David Fletcher 2018 年 4 月 15 日
Sorry, I meant replace the column list only, not the row indexing colon as well
bondpen
bondpen 2018 年 4 月 15 日
Yea, that does the trick. What is ":," for? Does it tell command to choose columns?
David Fletcher
David Fletcher 2018 年 4 月 15 日
What the first colon? It means all rows and columns 5,10,15...etc. If you don't have the first colon - as you have found - it will compact all selected columns into one row. Well that was a bit harder than it should have been, I think I need a lie down.

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

その他の回答 (0 件)

カテゴリ

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

タグ

質問済み:

2018 年 4 月 15 日

コメント済み:

Jan
2018 年 4 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by