Extracting a range from a cell array.

28 ビュー (過去 30 日間)
Erwin Werkhoven
Erwin Werkhoven 2022 年 1 月 1 日
編集済み: Stephen23 2022 年 1 月 3 日
Hi,
Consider this example:
a = ["a", "b", "c", "d", "e"]
b = a(3:end)
x = {"a", "b", "c", "d", "e"}
y = x(3:end)
z = x{3:end}
It gives the following output:
a =
1×5 string array
"a" "b" "c" "d" "e"
b =
1×3 string array
"c" "d" "e"
x =
1×5 cell array
{["a"]} {["b"]} {["c"]} {["d"]} {["e"]}
y =
1×3 cell array
{["c"]} {["d"]} {["e"]}
z =
"c"
I'd expect that z = x{3:end} would give the same 1x3 string array as b. Why does it only extract element "c"?
And how can I get result b (the 1x3 string array) from x?
Thanks in advance.
  2 件のコメント
Stephen23
Stephen23 2022 年 1 月 1 日
編集済み: Stephen23 2022 年 1 月 1 日
"I'd expect that z = x{3:end} would give the same 1x3 string array as b."
Nope, that is not how curly braces work with cell arrays: curly braces do NOT automagically concatenate anything together (luckily, otherwise we would be very limited in how we could use cell arrays containing different data types).
"Why does it only extract element "c"?"
Because you are generating a comma-separated list from the contents of the cell array:
"And how can I get result b (the 1x3 string array) from x?"
SImply concatenate the scalar strings (in your comma-separated list) into one string array:
[x{3:end}]
Erwin Werkhoven
Erwin Werkhoven 2022 年 1 月 1 日
Ok thanks for the explanation!

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

採用された回答

DGM
DGM 2022 年 1 月 1 日
編集済み: DGM 2022 年 1 月 1 日
How about
x = {"a", "b", "c", "d", "e"}
x = 1×5 cell array
{["a"]} {["b"]} {["c"]} {["d"]} {["e"]}
z = horzcat(x{3:end})
z = 1×3 string array
"c" "d" "e"
% or just
z = [x{3:end}]
z = 1×3 string array
"c" "d" "e"
To clarify, the expression x{3:end} has three outputs. When only a single output is requested, you'll only get the first one of the three.
  5 件のコメント
Erwin Werkhoven
Erwin Werkhoven 2022 年 1 月 3 日
編集済み: Erwin Werkhoven 2022 年 1 月 3 日
Ok thanks, so a = ["a", "b", "c", "d", "e"] gives an array (or vector, whatever it's called in matlab) of separate strings, but b = ['a', 'b', 'c', 'd', 'e'] merges everything into a single char vector 'abcde'. That's a bit confusing, but good to know.
Stephen23
Stephen23 2022 年 1 月 3 日
編集済み: Stephen23 2022 年 1 月 3 日
Erwin Werkhoven: with MATLAB basically everything is an array:
Even scalar numbers are arrays:
X = 3
X is an array which has size 1x1(x1x1...). Square brackets always concatenate things together: this is exactly the same for every single array type (e.g. numeric, char, string, cell, struct). So this:
X = [1,2,3]
really just concatenates three 1x1 arrays into a 1x3 array.
The only thing you need to pay attention to, is what you are concatenating together. In both of your examples the separate string arrays and character arrays respectively are concatenated into larger string and character arrays. Absolutely no difference whatsoever, the square brackets always concatenate.
This is a 1x1 numeric array: 2
This is a 1x1 numeric array: 3
If we concatenate them together horizontally, we will get a 1x2 numeric array.
This is a 1x1 character array: 'a'
This is a 1x1 character array: 'x'
If we concatenate them together horizontally, we will get a 1x2 numeric array.
This is a 1x1 string array: "x"
This is a 1x1 string array: "hello world".
If we concatenate them together horizontally, we will get a 1x2 string array.
etc. etc for every other array type.
Note that the number of characters (i.e. content) does NOT determine the size of a string array (this also applies to other container array types too, e.g. cell arrays and structure arrays). Do not mix up the size of the content of a container array with the size of the container array itself:
The documentation clearly states that string is a container type.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by