inserting headers into a vector
5 ビュー (過去 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 件のコメント
Sean de Wolski
2011 年 4 月 14 日
Can you please reformat your code so it's readable using the markup help?
回答 (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!