Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Inserting/Pushing a New Value into a Vector
Replies: 18   Last Post: Sep 19, 2013 10:54 AM

 Messages: [ Previous | Next ]
 Tiger Posts: 83 Registered: 6/22/12
Inserting/Pushing a New Value into a Vector
Posted: Sep 3, 2013 10:04 AM

Hi all,

Here's a sample vector, Vec = [6, 2 , 1 , 6 , 5 , 2 , 1 , 5 , 3 , 1 , 5 , 4 , 1 , 6 , 4 , 1];
I'd like to able to add, NewVal = 6, straight after the occurance of element "1", and if "6" already exists after it, then do nothing. Basically, the output I'm looking for is somehting like this:

Output, NewVec = 6 2 1 6 5 2 1 6 5 3 1 6 5 4 1 6 4 1;

I've tried all the tips on-line;
__________________________________________________________________
First Attempt:
clc; clear
PathNow = [6,2,1,6,5,2,1,5,3,1,5,4,1,6,4,1];
Unique_tIDs = PathNow(1,1);
end

The output here was:
6 2 1 6 5 2 1 5 3 1 5 4 1 6 6 4 1
__________________________________________________________________
Second Attempt:
clc; clear
PathNow = [6,2,1,6,5,2,1,5,3,1,5,4,1,6,4,1];
Unique_tIDs = PathNow(1, 1);
NewRecord = nan(size(c));
NewRecord(~c) = PathNow;
NewRecord(c) = Unique_tIDs

The output was:
6 2 6 1 6 5 6 2 1 6 5 3 6 1 5 4 1 6 4 1
__________________________________________________________________
Third, and final, Attempt:
clc; clear
PathNow = [6,2,1,6,5,2,1,5,3,1,5,4,1,6,4,1]; % A
Unique_tIDs = 6; % B
Unique_tIDs_i = Unique_tIDs_i(1:end-1)
PathNowNew = zeros(1, length(PathNow) + length(Unique_tIDs_i)) + NaN; % or Unique_tIDs_i
PathNowNew(Unique_tIDs_i+length(Unique_tIDs_i)-1) = Unique_tIDs;
PathNowNew(isnan(PathNowNew)) = PathNow

The two outputs were:
PathNow: 6 2 1 6 5 2 1 5 3 1 5 4 1 6 4 1
PathNowNew: 6 2 1 6 5 6 2 1 5 6 3 1 6 5 4 6 1 6 4 1

Any tips would be much appreciated, ta.

Date Subject Author
9/3/13 Tiger
9/3/13 Steven Lord
9/3/13 dpb
9/3/13 Tiger
9/3/13 dpb
9/6/13 Tiger
9/3/13 james bejon
9/6/13 Tiger
9/6/13 james bejon
9/7/13 Tiger
9/8/13 james bejon
9/9/13 Tiger
9/10/13 james bejon
9/18/13 Tiger
9/18/13 Tiger
9/18/13 james bejon
9/19/13 Tiger
9/19/13 Tiger
9/3/13 Roger Stafford