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

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일
편집: Stephen23 2020년 9월 3일
find is not required. Logical indexing is simpler and more efficient, you should just use index directly.
when I do this my
index2 == index1
will the latter part work normally?
You don't need index2 at all, just use index.
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

Okay, I see. So parentheses are for replacing while {} these are for accessing cell contents am I right?
oversikt.Jernbanen(1,index) = {'0'};
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.
I will search it up and read more about it. thank you so much for your time :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2020년 9월 3일

편집:

2020년 9월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by