Replace values in large matrix using If else

I am currently using dataset matrix which contains continuous data. I need to use If else to replace all the data that less than -1.53 to -1, data that more than 1.1366 to 1 and for others, it will be replce by 0. Any solution?

回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2011 年 11 月 2 日

0 投票

You could use if-else, but using index is easier and more efficient. It applies to dataset array or ordinary array. Use this example:
d=rand(8,9);
index1=d<0.3;
index2=d>0.7;
index3=and(~index1,~index2);
d(index1)=-1;
d(index2)=1;
d(index3)=0;

1 件のコメント

Jan
Jan 2011 年 11 月 2 日
You can omit index3 by: d2 = zeros(size(d)); d2(index1)=-1; d2(index2)=1;

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

タグ

質問済み:

2011 年 11 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by