I want to clean a decay by setting all values to zero after the first negative, please assist me on how to achieve that

20 次查看(过去 30 天)
B =
0.0374 0.0216 0.0185 -0.0320 -0.0012 0.0000 0.0063
0.0311 0.0157 0.0177 -0.0364 0.0029 -0.0022 0.0057
0.0244 0.0093 0.0170 -0.0400 -0.0044 -0.0044 0.0050
0.0178 0.0027 0.0164 -0.0428 -0.0056 -0.0065 0.0042
0.0114 -0.0039 0.0160 -0.0448 -0.0064 -0.0084 0.0034
for matrix B i want to set all values in a row after negative to zero.
e.g row 1 should look like;
0.0374 0.0216 0.0185 0.000 0.0000 0.0000 0.0060
OR row 2 to be;
0.0311 0.0157 0.0177 0.0000 0.0000 0.0000 0.0000

采纳的回答

Sameer
Sameer 2024-10-18
编辑:Sameer 2024-10-18
To transform the matrix "B", so that all values from the first negative value onwards in each row are set to zero, you can loop through each row, find the first negative value using the "find" function, and set that value and all subsequent values in the row to zero.
Here's how you can do it:
B = [
0.0374 0.0216 0.0185 -0.0320 -0.0012 0.0000 0.0063;
0.0311 0.0157 0.0177 -0.0364 0.0029 -0.0022 0.0057;
0.0244 0.0093 0.0170 -0.0400 -0.0044 -0.0044 0.0050;
0.0178 0.0027 0.0164 -0.0428 -0.0056 -0.0065 0.0042;
0.0114 -0.0039 0.0160 -0.0448 -0.0064 -0.0084 0.0034
];
% Loop through each row
for i = 1:size(B, 1)
% Find the index of the first negative value
first_neg_idx = find(B(i, :) < 0, 1);
% If a negative value is found, set it and all subsequent values to zero
if ~isempty(first_neg_idx)
B(i, first_neg_idx:end) = 0;
end
end
disp(B);
0.0374 0.0216 0.0185 0 0 0 0 0.0311 0.0157 0.0177 0 0 0 0 0.0244 0.0093 0.0170 0 0 0 0 0.0178 0.0027 0.0164 0 0 0 0 0.0114 0 0 0 0 0 0
Please refer to the below MathWorks documentation link:
Hope this helps!

更多回答(1 个)

Star Strider
Star Strider 2024-10-18
Use logical operations and the cumprod function for this —
B = [0.0374 0.0216 0.0185 -0.0320 -0.0012 0.0000 0.0063
0.0311 0.0157 0.0177 -0.0364 0.0029 -0.0022 0.0057
0.0244 0.0093 0.0170 -0.0400 -0.0044 -0.0044 0.0050
0.0178 0.0027 0.0164 -0.0428 -0.0056 -0.0065 0.0042
0.0114 -0.0039 0.0160 -0.0448 -0.0064 -0.0084 0.0034];
multmtx = cumprod(B >= 0, 2) % Logical Matrix Multiplied Cumulatively In The Column (2) Dimension
multmtx = 5×7
1 1 1 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B = B .* multmtx % Multiply By The Original ‘B’ Matrix
B = 5×7
0.0374 0.0216 0.0185 0 0 0 0 0.0311 0.0157 0.0177 0 0 0 0 0.0244 0.0093 0.0170 0 0 0 0 0.0178 0.0027 0.0164 0 0 0 0 0.0114 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
.
  4 个评论
Star Strider
Star Strider 2024-10-23,20:04
Thank you!
It’s also efficient, and while I kept the two statements separate to demonstrate how it works, they could be combined into a single statement —
B = [0.0374 0.0216 0.0185 -0.0320 -0.0012 0.0000 0.0063
0.0311 0.0157 0.0177 -0.0364 0.0029 -0.0022 0.0057
0.0244 0.0093 0.0170 -0.0400 -0.0044 -0.0044 0.0050
0.0178 0.0027 0.0164 -0.0428 -0.0056 -0.0065 0.0042
0.0114 -0.0039 0.0160 -0.0448 -0.0064 -0.0084 0.0034];
B = B .* cumprod(B >= 0, 2) % Multiply The Original ‘B’ Matrix By The Logical Matrix Multiplied Cumulatively In The Column (2) Dimension
B = 5×7
0.0374 0.0216 0.0185 0 0 0 0 0.0311 0.0157 0.0177 0 0 0 0 0.0244 0.0093 0.0170 0 0 0 0 0.0178 0.0027 0.0164 0 0 0 0 0.0114 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A vote for it would be appreciated!

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by