필터 지우기
필터 지우기

How do I prevent overwriting of matrix elements?

조회 수: 9 (최근 30일)
Domantas Laurinavicius
Domantas Laurinavicius 2021년 2월 4일
편집: Stephen23 2021년 2월 5일
The matrix elements start writing correctly, but as soon as the next one gets written, all preceeding elements also get the next value applied to them for some reason. This just leaves me with the last value of the V vector as all first column and row elements
n = 5; % Matrix will be (n+1)x(n+1)
V = ZeroBased(zeros(1,n));
for i = 1:n
V(i) = i/3;
end
H = ZeroBased(zeros(n));
for cols = 0:n
for rows = 0:n
if cols == 0 % Only column 1
H(1:rows,cols) = V(rows);
elseif rows == 0 % Only row 1
H(rows,1:cols) = V(cols);
elseif rows == cols % Only diagonal (excluding 1,1 for some reason)
H(rows,cols) = rows^2 + sum(V);
else
H(rows,cols) = V(rows); % Remainder of cells
end
H(0,0) = sum(V_vals) % Necessary to have an exception statement for 1,1
end
end
I've included comments so you can see my train of thought to some degree.
  댓글 수: 3
Domantas Laurinavicius
Domantas Laurinavicius 2021년 2월 4일
The ZeroBased() comes from a separate pack that makes the coding very convenient but it's not necessary. Making the vector and matrix normal doesn't change the problem.
Stephen23
Stephen23 2021년 2월 5일
편집: Stephen23 2021년 2월 5일
"that makes the coding very convenient but it's not necessary"
What is the task?

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

채택된 답변

David Goodmanson
David Goodmanson 2021년 2월 4일
Hi Domantas
This is just a provisional answer since I am not sure exactly what you are trying to do, but statements like
H(1:rows,cols) = V(rows); and H(rows,1:cols) = V(cols);
ensure that previous rows and columns of H are going to be overwritten, as opposed to
H(rows,cols) = whatever
  댓글 수: 5
Stephen23
Stephen23 2021년 2월 5일
편집: Stephen23 2021년 2월 5일
"I don't understand how further cycles of the loop can replace preceeding matrix elements since I can see it changing the matrix cell by cell."
Because that is exactly what your indexing does. Consider the first row when cols = n, then:
H(rows,1:cols) = some value
% ^^^^ first row
% ^^^^^^ all columns <- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
You told MATLAB to put some value into all columns of the first row. So it does exactly that.
All previous iterations are totally irrelevant, because on that last cols iteration you replace the value of every cell in that row with the same value. And that value is the V(cols).
"Also, the statement ... takes the value of (rows) at the current matrix element and applies V(rows) to the cell without overwriting preceeding values."
H(rows,cols) = some value
% ^^^^ some row
% ^^^^ some column
Different indexing, different result. No surprises there.
"Thank you for responding, hope you can help further!"
What are you trying to implement?
Domantas Laurinavicius
Domantas Laurinavicius 2021년 2월 5일
편집: Domantas Laurinavicius 2021년 2월 5일
Oh, huh, it was that simple. Thanks very much!
Edit, this is the equation I'm trying to fit to the matrix:

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 2월 5일
To prevent overwriting of matrix elements, you could define a new class that stored an array, and also stored a map of which elements had been written to so far. Define a subsasgn method for it that checked the subscripts to be assigned to against the list of locations already written to, and then do one of:
  • issue an error
  • issue a warning and skip re-assigning to those particular locations and continue
  • skip re-assigning to those locations and silently continue
You would also want to define all the usual mathematical operations on the array.
When you do define the usual mathematical operations, one question would be what you want to do if the operation requests read access to locations that the user had not requested to write to yet.

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by