How to replace multiple values with the same value in a cell array?

12 ビュー (過去 30 日間)
iseinas
iseinas 2020 年 9 月 3 日
編集済み: iseinas 2020 年 9 月 11 日
I would like to replace multiple values with the same value in a cell array.
% retrieve a vector with logical values
index = contains(oversikt.Jernbanen, '395');
% find the indexes of the index vector where the logical values = 1
index2 = find(index == 1);
% replace all the values. / the error: "Expected one output from a curly brace or dot indexing expression, but there were 12 results."
oversikt.Jernbanen{1,index2} = '0';
  4 件のコメント
Stephen23
Stephen23 2020 年 9 月 3 日
You don't need index2 at all, just use index.
iseinas
iseinas 2020 年 9 月 3 日
Okay thank you so much!

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

採用された回答

Stephen23
Stephen23 2020 年 9 月 3 日
編集済み: Stephen23 2020 年 9 月 3 日
The RHS must be a scalar cell.
The LHS must use parentheses, because you are replacing cells (not accessing their contents).
oversikt.Jernbanen(1,index) = {'0'};
Remember:
  • () parentheses refer to the cells themselves
  • {} curly braces refer to the cell contents.
  4 件のコメント
Stephen23
Stephen23 2020 年 9 月 3 日
編集済み: Stephen23 2020 年 9 月 3 日
"... {} these are for accessing cell contents"
Correct.
"So parentheses are for replacing ..."
Parenthese are not just "for replacing". Parentheses refer to the elements (i.e. cells) of a cell array. You can use them for many things, e.g. getting a sub-array of a cell array, assigning new cells, etc., exactly like you can use parenthesis indexing with any other array class.
iseinas
iseinas 2020 年 9 月 3 日
I will search it up and read more about it. thank you so much for your time :)

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by