Add values at specific index in a cell

22 ビュー (過去 30 日間)
David Haydock
David Haydock 2022 年 1 月 11 日
回答済み: David Haydock 2022 年 1 月 12 日
I have an n x 1 cell array, and a list of indexes for this cell array that need to be used to insert new values into the cell array, at those given indexes. My problem is that as soon as I add a value at a given index, the length of the cell array is going to change, and then my next index is invalid.
How would I go about doing this? Any help would be appreciated!
  3 件のコメント
Cris LaPierre
Cris LaPierre 2022 年 1 月 11 日
Provide an example, please.
David Haydock
David Haydock 2022 年 1 月 12 日
編集済み: David Haydock 2022 年 1 月 12 日
%Here is an example
% take some cell array
cell_arr = {'A', 'B', 'C', 'D', 'E', 'F'}
% and some set of indexes
idx = [1, 3, 4]
% I need to insert a value, '\n' in cell_arr at 1, 3 and 4.
% if I loop this, adding in '\n' at position 1 increases the size of the
% array by 1, and means that where I previously had to add '\n' to position
% 3, it would now be in position 4, and so on.
% I do not wish to replace the values at each of these indexes, but to add
% in values at these indexes.

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

採用された回答

David Haydock
David Haydock 2022 年 1 月 12 日
Answer is here https://uk.mathworks.com/matlabcentral/answers/402575-how-to-insert-an-element-into-any-index-of-vector

その他の回答 (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