How do I prevent overwriting of matrix elements?

7 vues (au cours des 30 derniers jours)
Domantas Laurinavicius
Domantas Laurinavicius le 4 Fév 2021
Modifié(e) : Stephen23 le 5 Fév 2021
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 commentaires
Domantas Laurinavicius
Domantas Laurinavicius le 4 Fév 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 le 5 Fév 2021
Modifié(e) : Stephen23 le 5 Fév 2021
"that makes the coding very convenient but it's not necessary"
What is the task?

Connectez-vous pour commenter.

Réponse acceptée

David Goodmanson
David Goodmanson le 4 Fév 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 commentaires
Stephen23
Stephen23 le 5 Fév 2021
Modifié(e) : Stephen23 le 5 Fév 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 le 5 Fév 2021
Modifié(e) : Domantas Laurinavicius le 5 Fév 2021
Oh, huh, it was that simple. Thanks very much!
Edit, this is the equation I'm trying to fit to the matrix:

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 5 Fév 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.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by