Dynamically adding element to an array

조회 수: 26 (최근 30일)
Darren Lim
Darren Lim 2021년 1월 6일
댓글: Darren Lim 2021년 1월 6일
Dear Community,
Still new to matlab and exploring, It may be a simple problem but I am stuck for sometime on trying to come out with a clean logic .
Say i have an array (N X 2 )
signal price
1 29.21
-1 29.41
-1 29.43
-1 29.63
1 29.01
0 29.21
1 28.62
1 28.82
I wanted to add a row if the consecutive row in column 1 is -1 to 1 or -1 to 1 , the row to be added is a 0 and column 2 will be the price in the next row.
Basically a 1 is a long signal and -1 is a short signal and 0 is exit a position (in finance). The problem with the original data is when the scenario is from long to short ( 1 to -1) , i need to exit it before shorting , so i need to add a row with 0 and the price to exit.
The result I am expecting will be :
signal price
1 29.21
0 29 41 (added row)
-1 29.41
-1 29.43
-1 29.63
0 29.01 (added row)
1 29.01
0 29.21
1 28.62
1 28.82
The code I attempted :
spread = y; %price
temp=NaN(length(spread), 2); % initialise N X 2 array
temp(:,2)=spread; % put price into 2nd column
%numUnits is a single row of signals (eg : -1,0,1...)
for i = 2:numel(numUnits)
temp(i,1)=numUnits(i);
compare = abs(numUnits(i)-numUnits(i-1));
if compare == 2
temp = [temp(1:i-1,:); temp(i:i,:); temp(i:end,:)];
temp(i,1)=0;
%temp(i,2)=i;
end
end
The result is incorrect because the for loop is always operating on the original NumUnits array and not the Temp where I am adding the extra rows. I dont have an elegant solution to add dynamically an array and operate it on the fly.
Anyone able to help or give some clue? Thanks in advance!
Darren

채택된 답변

Timo Dietz
Timo Dietz 2021년 1월 6일
편집: Timo Dietz 2021년 1월 6일
priceArray= ...
[1 29.21
-1 29.41
-1 29.43
-1 29.63
1 29.01
0 29.21
1 28.62
1 28.82]
y=priceArray(:, 2);
numUnits = priceArray(:, 1);
% YOUR CODE WITH MODIFICATIONS:
spread = y; %price
temp=NaN(2*length(spread), 2); % initialise N X 2 array TWICE the size of the origin
%temp(:,2)=spread; % put price into 2nd column
%numUnits is a single row of signals (eg : -1,0,1...)
idx = 1; % introduce second index variable
for i = 2:numel(numUnits)
%temp(i,1)=numUnits(i);
compare = abs(numUnits(i)-numUnits(i-1))
if compare == 2
temp(idx, :) = priceArray(i-1, :);
idx = idx + 1;
temp(idx, :) = [0 priceArray(i, 2)];
else
temp(idx, :) = priceArray(i-1, :);
end
idx = idx + 1;
end
temp(idx, :) = priceArray(i, :); % add last row which always equals the origin
temp = temp(1:idx, :)
Try these modifications. The point is that you have to introduce a second index variable since your temp array is bigger than
the origin. So you can't use one common index. Further, the initialization of temp must reserve more rows - I chose twice
the size for simplicity (+50% should suffice already).
  댓글 수: 1
Darren Lim
Darren Lim 2021년 1월 6일
thank you Timo, this works! on my learning journey and this helps a lot!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by