필터 지우기
필터 지우기

How do I display only positive numbers from a matrix, i.e column 1 has to be positive to display the corresponding 3 columns of data?

조회 수: 21 (최근 30일)
I have column 1 which shows the index returns for each day, the next 3 columns are stock price returns from the corresponding day. There are currently days displaying a negative return for the index and the 3 stock prices next to it. I want to code that I only want to display the positive index returns and the corresponding stock prices of that same day.
  댓글 수: 2
Christopher Wolfe
Christopher Wolfe 2021년 9월 12일
The returns are denoted in this line rets = log(price(2:end,:)./price(1:end-1,:));
Ive J
Ive J 2021년 9월 12일
A = [randi([-10 10], 4, 1), randi([20 100], 4, 3)]
A = 4×4
3 20 63 45 -6 20 45 45 -6 33 54 80 10 99 48 61
idx = A(:, 1) > 0; % positive indices only
newA = A(idx, :)
newA = 2×4
3 20 63 45 10 99 48 61

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

답변 (1개)

Walter Roberson
Walter Roberson 2021년 9월 12일
mask = find(rets >= 0);
prets = rets(mask);
pprice = price(mask+1,:);
rets(K) is calculated based upon both price(K+1,:) and price(K,:) so it is not clear which of the two days you would want to have displayed. Here I chose to have it associated with the second day rather than the first.
  댓글 수: 3

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by