How do I prevent overwriting of matrix elements?

7 visualizzazioni (ultimi 30 giorni)
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 Commenti
Domantas Laurinavicius
Domantas Laurinavicius il 4 Feb 2021
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 il 5 Feb 2021
Modificato: Stephen23 il 5 Feb 2021
"that makes the coding very convenient but it's not necessary"
What is the task?

Accedi per commentare.

Risposta accettata

David Goodmanson
David Goodmanson il 4 Feb 2021
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 Commenti
Stephen23
Stephen23 il 5 Feb 2021
Modificato: Stephen23 il 5 Feb 2021
"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 il 5 Feb 2021
Modificato: Domantas Laurinavicius il 5 Feb 2021
Oh, huh, it was that simple. Thanks very much!
Edit, this is the equation I'm trying to fit to the matrix:

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 5 Feb 2021
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.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by