Adding element within vector without overwriting the existing value

Is there a way of inserting values at points within an existing vector/array without overwriting the existing value?
So I have a list of values and I have a vector with positions in this list, is there a way of inserting the new values at these positions?
For example, is there a way of taking this list
expitemorder =
'Strong_Alt_Prime2.png'
'Strong_Alt_Target2.png'
'Strong_Alt_Prime1.png'
'Strong_Alt_Target1.png'
'Strong_Alt_Prime3.png'
'Strong_Alt_Target3.png'
'Strong_Alt_Prime4.png'
'Strong_Alt_Target7.png'
and inserting new/additional values without overwriting the existing values to get something like this
expitemorder =
'Strong_Alt_Prime2.png'
'Strong_Alt_Target2.png'
'NEW VALUE'
'Strong_Alt_Prime1.png'
'Strong_Alt_Target1.png'
'Strong_Alt_Prime3.png'
'Strong_Alt_Target3.png'
'NEW VALUE'
'Strong_Alt_Prime4.png'
'Strong_Alt_Target7.png'
So a way of adding new rows at predetermined positions within the list? Not just vertically concatenating the two.
Thanks

回答 (4 件)

Geoff Hayes
Geoff Hayes 2015 年 2 月 16 日
Alice - short of defining a special class to handle this sort of insertion into a list (i.e a linked list), I think that concatenation is one of the ways to go about creating your new list. So if
expitemorder = {...
'Strong_Alt_Prime2.png'
'Strong_Alt_Target2.png'
'Strong_Alt_Prime1.png'
'Strong_Alt_Target1.png'
'Strong_Alt_Prime3.png'
'Strong_Alt_Target3.png'
'Strong_Alt_Prime4.png'
'Strong_Alt_Target7.png'};
and you wish to insert a new string into the third position, then the code to do this would be
expitemorder = [expitemorder(1:2) ; 'NEW_VALUE' ; expitemorder(3:end)]
which produces
expitemorder =
'Strong_Alt_Prime2.png'
'Strong_Alt_Target2.png'
'NEW_VALUE'
'Strong_Alt_Prime1.png'
'Strong_Alt_Target1.png'
'Strong_Alt_Prime3.png'
'Strong_Alt_Target3.png'
'Strong_Alt_Prime4.png'
'Strong_Alt_Target7.png'

2 件のコメント

Alice
Alice 2015 年 2 月 16 日
Is it possible to use this to insert a new variable at multiple positions without repeating the previous sections of the list?
Geoff Hayes
Geoff Hayes 2015 年 2 月 16 日
Alice - please clarify what you mean by without repeating the previous sections of the list. What are the previous sections?

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

Thorsten
Thorsten 2015 年 2 月 16 日
X = {'asdf' 'foo' 'bar'};
n = 3; name = 'foooo';
X = {X{1:n-1} name X{n:end}};

2 件のコメント

Alice
Alice 2015 年 2 月 16 日
How would you implement this when n is a vector? So the resulting list would not have duplicated information from above?
Thorsten
Thorsten 2015 年 2 月 18 日
編集済み: Thorsten 2015 年 2 月 18 日
There is probably a more elegant way w/o a for-loop, but this works:
X = {'asdf' 'foo' 'bar' 'yuck'};
pos = [2 4]; name = {'hi' 'this is new'};
for i = 1:numel(pos)
n = pos(i);
X = {X{1:n-1} name{i} X{n:end}};
end

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

Stephen23
Stephen23 2015 年 2 月 16 日
編集済み: Stephen23 2015 年 2 月 17 日
This is easy using some MATLAB indexing and mat2cell, and just three lines of code:
X = [2,6]; % insert new element AFTER these elements.
A = {'Strong_Alt_Prime2.png',...
'Strong_Alt_Target2.png',...
'Strong_Alt_Prime1.png',...
'Strong_Alt_Target1.png',...
'Strong_Alt_Prime3.png',...
'Strong_Alt_Target3.png',...
'Strong_Alt_Prime4.png',...
'Strong_Alt_Target7.png'};
B = mat2cell(A,1,[X(1),diff(X),numel(A)-X(end)]);
B(2,:) = {'NEW VALUE'}; % new element
B = [B{1:end-1}];
When you run this code, the value of B will be:
B = {'Strong_Alt_Prime2.png'
'Strong_Alt_Target2.png'
'NEW VALUE'
'Strong_Alt_Prime1.png'
'Strong_Alt_Target1.png'
'Strong_Alt_Prime3.png'
'Strong_Alt_Target3.png'
'NEW VALUE'
'Strong_Alt_Prime4.png'
'Strong_Alt_Target7.png'}
Of course you can define X to be any positions for the new element, given the following conditions:
  • X cannot be empty.
  • all values in X are 0<=X<=numel(A).
  • the values are sorted in order.
  • zero places a new element before the first element.
  • accepts multiple new elements in the same location.
Ajay Pherwani
Ajay Pherwani 2015 年 2 月 16 日

0 投票

expitemorder is a 8x1 cell array/vector , to update any element : - expitemorder{ any_index,1}='value';

1 件のコメント

Thorsten
Thorsten 2015 年 2 月 18 日
No, this overwrites the value at any_index , but Alice asked for without overwriting the existing values.

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

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2015 年 2 月 16 日

編集済み:

2015 年 2 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by