### Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

## Replacing values based on a condition in a matrix.

Megan Stapley

### Megan Stapley (view profile)

さんによって質問されました 2019 年 3 月 20 日

### Kevin Phung (view profile)

さんによって コメントされました 2019 年 3 月 20 日
Hi there!
I have a 2by2 matrix and am trying to replace every value in the second matrix that is greater than 20 with "on".
So basically I want something like this:
1 2
2 30
30 50
40 10
55 15
to change to this:
1 closed
2 open
30 open
40 closed
55 closed
If anyone can help that would be great! Thanks!

#### 0 件のコメント

サインイン to comment.

R2016b

## 1 件の回答

2019 年 3 月 20 日

### Kevin Phung (view profile)

2019 年 3 月 20 日

ind = M(:,2) > 20; %grab indices along second column where value is > 20
M = num2cell(M); % youre going to need to turn it into a cell array
M(ind,2) = {'open'}; %set those indices to open
M(~ind,2) = {'closed'}; %set indices that are not to closed
M
let me know if this helps. Also, I suggest you read about cell arrays:

Megan Stapley

### Megan Stapley (view profile)

2019 年 3 月 20 日
Thank you so much this is exactly what I was looking for! In addition, how would I add titles to these columns?
Walter Roberson

### Walter Roberson (view profile)

2019 年 3 月 20 日
M = [{'title for first column', 'title for second column'}; M];
Kevin Phung

### Kevin Phung (view profile)

2019 年 3 月 20 日
Happy to help!
As for adding titles to the column, Walter's comment would work.
Another option is to turn the cell matrix into a table:
ind = M(:,2) > 20; %grab indices along second column where value is > 20
M = num2cell(M); % youre going to need to turn it into a cell array
M(ind,2) = {'open'}; %set those indices to open
M(~ind,2) = {'closed'}; %set indices that are not to closed
M = table(M(:,1),M(:,2),'VariableNames',{'Name1','Name2'})

サインイン to comment.

Translated by