An easy way to modify matrix elements?

조회 수: 1 (최근 30일)
Andrew Rutter
Andrew Rutter 2021년 12월 24일
댓글: Andrew Rutter 2021년 12월 25일
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
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?
Andrew Rutter
Andrew Rutter 2021년 12월 25일
Yes sorry, it is hard to write clearly, Failure means product fails in development, typically after 4 years, so in my example the first and third make it into production and the second fails, hence it is made for 4 years then has zero production, In P, 1 represents it being made and 0 a fail.
As one product starts development each year, this produces a zero in column 1 and 2 for second and third products as they havent started development.
hope this makes sense.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
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.
NCE2 = 3×20
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
  댓글 수: 2
Andrew Rutter
Andrew Rutter 2021년 12월 25일
This looks promising, I will give a try and get back to you...thank you for helping
Andrew Rutter
Andrew Rutter 2021년 12월 25일
Its works - thank you so much....best regards, Andrew

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Torsten
Torsten 2021년 12월 24일
편집: Torsten 2021년 12월 24일
for NCE = 1:NCEMAX
if Failure(NCE) == 0
P(NCE,NCE+4:end) = 0
end if
end

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by