Do not use the for loop to calculate the first number greater than 0 in each line

1 次查看(过去 30 天)
x=randn(10,10)
...
  2 个评论
Jan
Jan 2022-11-19
This sounds like a homework question. So please show, what you have tried so far and ask a specific question about Matlab. The forum will not solve your homework, but assist you in case of questions concerning Matlab.
slevin Lee
slevin Lee 2022-11-19
编辑:Jan 2022-11-19
x=randn(10,10)
x1=x>0
x2=cumsum(x1,2)
x3=x2==0
x4=sum(x3,2)+1
x5=x(:,x4).*eye(10,10)
answer=sum(x5,2)
sorry!
I just want to see if there's a better way

请先登录,再进行评论。

采纳的回答

Jan
Jan 2022-11-19
编辑:Jan 2022-11-19
x = randn(10, 10)
y = x.'; % Required for the last step y(y3==1)
y1 = y > 0
y2 = cumsum(y1)
% Now a row can contain multiple 1's. Another CUMSUM helps:
y3 = cumsum(y2)
y(y3 == 1)
Another way:
x = randn(10, 10)
m = x > 0
y = cumsum(x .* m, 2)
y(~m) = Inf % Mask leading zeros
min(y, [], 2)
Or with beeing nitpicking for the formulation "no for loop":
result = zeros(1, 10);
m = x > 0;
k = 1;
while k <= 10
row = x(k, :);
result(k) = row(find(row(m(k, :)), 1));
k = k + 1;
end
And another method:
[row, col] = find(x > 0);
first = splitapply(@min, col, row);
x(sub2ind(size(x), 1:10, first.'))

更多回答(1 个)

DGM
DGM 2022-11-19
编辑:DGM 2022-11-19
Here's one way.
x = randn(10,10)
x = 10×10
0.5355 -1.4904 -0.9662 -0.7693 0.6789 0.6031 0.2375 -1.0743 0.6916 -0.8738 -0.0884 0.5313 -0.2913 1.0506 -1.1930 0.1070 0.2047 -0.2512 2.0512 0.1435 -0.1235 0.5840 1.3542 -0.3692 1.1048 -0.4211 -2.1762 -0.3553 0.8296 -2.1670 -0.6151 0.7344 -1.1325 -1.9521 -1.5595 0.8968 -0.8484 -2.5253 -0.1668 1.8909 1.0192 -0.4541 0.9363 -0.1635 -1.7753 -0.2266 1.1185 2.0062 -1.3746 -0.3283 -0.2484 -1.0466 1.2285 0.4551 -1.1110 -1.1021 0.3378 0.2945 -0.2886 1.0569 1.4228 -1.4178 0.5278 0.1037 0.7001 -0.3782 0.3081 1.4122 0.6484 -1.0982 -0.0277 -1.6063 -0.8334 -0.2916 -1.0505 -0.6081 -0.4252 -1.0511 0.4218 0.2551 -0.2180 0.5246 0.2145 1.4641 2.2411 -1.4396 -0.2848 -0.4727 2.5402 2.0645 0.5302 0.6045 -0.7767 1.2057 -0.9240 0.0234 1.1590 -0.0401 1.3279 0.5901
[~,idx] = min(x<=0,[],2) % note the logical inversion
idx = 10×1
1 2 2 2 1 3 1 9 2 1
firstnz = x(sub2ind(size(x),(1:size(x,1))',idx)) % get the values from the array
firstnz = 10×1
0.5355 0.5313 0.5840 0.7344 1.0192 1.2285 1.4228 0.4218 0.5246 0.5302

类别

Help CenterFile Exchange 中查找有关 Sources 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by