inserting headers into a vector

6 次查看(过去 30 天)
Boris
Boris 2011-4-14
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 个评论
Sean de Wolski
Sean de Wolski 2011-4-14
Can you please reformat your code so it's readable using the markup help?

请先登录,再进行评论。

回答(3 个)

Paulo Silva
Paulo Silva 2011-4-14
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]

Matt Fig
Matt Fig 2011-4-14
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 个评论
Boris
Boris 2011-4-14
I use function mod to find position where must be header.
sorry I made a mistake
n=0;
for j=1:(length(data)+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
I tried to split vector data to smaller vectors (1216 numbers) and between each smaller vector I wanted to insert header.
Matt Fig
Matt Fig 2011-4-14
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
Oleg Komarov 2011-4-14
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;

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by