inserting headers into a vector
2 views (last 30 days)
Show older comments
I have numbers 127 or 129 and I need insert some headers two numbers 27425 and 1216.
Header, data(1216 numbers), header...
I solved this problem using for cycle, but it was very slow (I have very long vector).
n=0;
for j=1:(length(input)+ceil(length(data)/1216)*2)
if j==1 || mod(j-1,1218)==0
output(j) = 27425;
elseif j==2 || mod(j-2,1218)==0
output(j) = 1216;
else
n=n+1;
output(j) = data(n);
end
end
Are there some functions to insert columns?
1 Comment
Sean de Wolski
on 14 Apr 2011
Can you please reformat your code so it's readable using the markup help?
Answers (3)
Paulo Silva
on 14 Apr 2011
v is your vector, val is just a number
insert column in the first index
v=[val v]
insert column in the last index
v=[v val]
insert values (headers), val1 and val2 are just numbers
v=[val1 v val2]
0 Comments
Matt Fig
on 14 Apr 2011
Boris, please format your code and fix the errors. When I tried to copy and paste your code, it looks like you are missing some operations.
n=0;
for j=1:(length(input)+ceil(length(data)/1216)*2)
if j==1 mod(j-1,1218)==0
output(j) = 27425;
elseif j==2 mod(j-2,1218)==0
output(j) = 1216;
else
n=n+1;
output(j) = data(n);
end
end
For example, what is the MOD function doing there? Did you mean to have an operator between the 1 and mod(j-1,1218)==0, and between the 2 and mod(j-2,1218)==0? If so, what operator?
2 Comments
Matt Fig
on 14 Apr 2011
But the mod function call is not doing anything here. It simply returns true or false when j==1 or j==2. It will not even execute after the first two passes through the loop, so I don't understand why it is even in there.
Oleg Komarov
on 14 Apr 2011
My solution, based on the assumption that headers don't have to be repmatted:
% Example input, stepsize and one header (multiple headers may require repmatting)
A = rand(9,1);
step = 4;
head = 1000;
% Create index of new positions
numA = numel(A);
idx = true(numA + fix(numA/step)+1, 1);
idx(1:step:end) = false;
% Reassign values to new positions and add header in between
A(idx) = A;
A(~idx) = head;
0 Comments
See Also
Categories
Find more on Time Series in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!