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 个评论
Ive J
2021-9-12
A = [randi([-10 10], 4, 1), randi([20 100], 4, 3)]
idx = A(:, 1) > 0; % positive indices only
newA = A(idx, :)
回答(1 个)
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 Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!