How to convert array to tuple?

111 ビュー (過去 30 日間)
Pontus Vikstål
Pontus Vikstål 2018 年 11 月 3 日
編集済み: Stephen23 2018 年 11 月 3 日
How can I convert an array
a = [1, 2, 3];
to a tuple
a = (1, 2, 3)
Or let's suppose that I have a matrix.
A = [1, 2; 3, 4]
and that I want to access the element of the second row first column element using an array [2, 1]. How can I do that? I.e. how can I get the element
A(2,1)
doing something like A([2,1]). I'm just looking for something similar to python which would be
A(tuple([1,2]))
  1 件のコメント
Stephen23
Stephen23 2018 年 11 月 3 日
編集済み: Stephen23 2018 年 11 月 3 日
"How to convert array to tuple?"
MATLAB does not have tuples. It has arrays of various classes. There are numeric arrays (e.g. double, single, int8, etc), or character arrays, or logical arrays, and some are container arrays (which can contain other arrays, e.g. cell, struct, table, etc).
It is not clear what you are trying to achieve with this:
A(tuple([1,2]))
What do you expect the output to be?

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

採用された回答

Walter Roberson
Walter Roberson 2018 年 11 月 3 日
MATLAB does not have tuples.
You can use sub2ind to convert multiple subscripts to linear indices.
MATLAB does have comma separated lists. It would be valid to do
T = num2cell(a) ;
A(T{:})
Unfortunately there does not appear to be any way in MATLAB to combine those two steps without using an intermediate true function, at least not in the general multidimensional case. You can always define something like
tuple2 = @(A,v) sub2ind(size(A), v(1), v(2))
After which you could use
A(tuple2(A, a))
But it hardly seems worth it.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by