An easy way to modify matrix elements?
1 次查看(过去 30 天)
显示 更早的评论
I've created a matrix, P, representing products (the rows set by NCE = 1:NCEMAX) over time (20 years - columns). I launch 1 product/year, some of which will fails 4 years into production (randomly 3x1 matrix). It might look like this without failure (a 1 represents in manufacture)
NCEMAX = 3;
P=[...
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
Failure = [1;0;1]
The matrix I want to create would be, as the second product fails 4 years after it starts (ie year 5 as it is NCE2)
NCE2 = [...
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
I've tried
P(: , NCE+4:end) = P(: , NCE+4:end) .* Failure;
but the NCE+4 doesn't shift up each row, i.e. NCE+4 always = 5 not 5,6 then 7.
What am I doing wrong? Any help much appreciated.
4 个评论
Image Analyst
2021-12-24
编辑:Image Analyst
2021-12-24
What does Failure represent? Like if it is 1 then that row fails for years 4 and later so P for that row would be 1 for columns 1,2 and 3, then 1 for 4-20?
What exactly is supposed to be shifting upwards? Some rows get shifted up, like row 3 moves into row 2 or something?????
So P=1 for no failure and P=0 for failure? And what value indicates failure in the failure vector 1 or 0?
Why does the first column and second of NCE2 have some zeros in some rows before the 1's start? Is that because the product has not yet been produced for those years?
采纳的回答
Image Analyst
2021-12-24
编辑:Image Analyst
2021-12-24
Perhaps this:
NCEMAX = 3;
P = ones(NCEMAX, 20);
Failure = [1;0;1];
[rows, columns] = size(P);
NCE2 = zeros(rows, columns);
for row = 1 : rows % For each year.
NCE2(row, row:end) = 1; % Initialize to no failure
if Failure(row) == 0
% Failed at year 4 after introduction
NCE2(row, row+4:end) = 0;
end
end
NCE2 % Show in command window.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!