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

27 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Sameer
Sameer am 18 Okt. 2024
Bearbeitet: Sameer am 18 Okt. 2024
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!

Weitere Antworten (1)

Star Strider
Star Strider am 18 Okt. 2024
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 Kommentare
Star Strider
Star Strider am 23 Okt. 2024 um 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!

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by