inserting headers into a vector
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(3 个)
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]
0 个评论
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 个评论
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
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;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!