Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   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
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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];
FirstTask = 1;
Unique_tIDs = PathNow(1,1);
LocFirstTask = find(PathNow==FirstTask);
LocFirstTask = LocFirstTask(1:end-1);
for i = 1:length(LocFirstTask)
LocFirstTask = find(PathNow==FirstTask);
LocFirstTask = LocFirstTask(1:end-1);
NewRecord = [diag(kron(PathNow.',ones(1,LocFirstTask(i))));Unique_tIDs;flipud(diag(kron(flipud(PathNow.'),ones(1,length(PathNow)-LocFirstTask(i)))))].'
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);
FirstTask = 1;
LocFirstTask = find(PathNow==FirstTask);
LocFirstTask = LocFirstTask(1:end-1);
c=false(1, length(PathNow)+length(LocFirstTask));
c(LocFirstTask)=true;
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
FirstTask = 1;
Unique_tIDs = 6; % B
Unique_tIDs_i = find(PathNow==FirstTask); % Bi
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.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.