How to remove a particular value (index) from an array? And also how to add a particular variable to an existing array?

8 ビュー (過去 30 日間)
hs(n) = {1,2,3,4} if i want to remove '3' from hs(n). What should i do? if i want to add '5' to hs(n). What should i do? Please use some variables to answer this ... Thanks in advance :)

採用された回答

KSSV
KSSV 2017 年 2 月 17 日
% if cell
hc = {1,2,3,4} ;
hc{3} = 5 ;
% if matrix
hm = [1 2 3 4] ;
hm(3) = 5 ;
  3 件のコメント
hariharan ilango
hariharan ilango 2017 年 2 月 17 日
I want to remove 'a' from hs(n). 'a' may be any one of the values from hs(n)
KSSV
KSSV 2017 年 2 月 17 日
Let i = 3 be the index you want to remove:
If cell:
hc = {1,2,3,4} ;
hc{3} = [] ;
hc(~cellfun('isempty',hc))
If matrix;
hc = [1 2 3 4] ;
hc(3) = []

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 2 月 17 日
編集済み: Walter Roberson 2017 年 2 月 17 日
mask = ismember(hs(n), 'a');
hs{n}(mask) = [] ;
Note that the order of arguments for the ismember for this purpose is the opposite of what you might expect.
Also this code expects a cell array of strings, consistent with your use of '3' but not consistent with your initial assignment which had a cell array of numeric values. You used the string form more so I programmed for that.

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by