How do I prevent overwriting of matrix elements?

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 Comments

hi
my first comment would be that matlab is not a zero based index language
this could lead to serious trouble in your code
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.
"that makes the coding very convenient but it's not necessary"
What is the task?

Sign in to comment.

 Accepted Answer

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 Comments

I've tried splitting the matrix up into different sections - Row 1 and Column 1, the diagonal, and the body of the matrix.
Each one has a slight variation of one main equation that I've tried bending to my will because I'm not sure how to implement Kronecker deltas into the code (MATLAB's version doesn't seem to work).
If we just consider Row 1, which each pass of the loop it outputs [V(1) 0 0 0] -> [V(2) V(2) 0 0] -> [V(3) V(3) V(3) 0] -> [V(4) V(4) V(4) V(4)] for however large my V vector is instead of [V(1) V(2) V(3) V(4)].
"I'm not sure how to implement Kronecker deltas into the code"
Generating a Kronecker delta matrix is quite easy, there are several common approaches (hint: mod, ndgrid, etc.) Using nested loops would not be a very effective use of MATLAB for this kind of task.
"MATLAB's version doesn't seem to work"
Basic MATLAB does not have a Kronecker delta function (there is a kroneckerdelta function in the symbolic toolbox).
"each pass of the loop it outputs [V(1) 0 0 0] -> [V(2) V(2) 0 0] -> [V(3) V(3) V(3) 0] -> [V(4) V(4) V(4) V(4)] for however large my V vector is instead of [V(1) V(2) V(3) V(4)]."
Of course, because that is exactly what you wrote your code to do. Consider the logic here:
for cols = 0:n
for rows = 0:n
..
elseif rows == 0 % Only row 1
H(rows,1:cols) = V(cols);
..
end
end
What happens for cols = 1, 2, ... n ?
What values will you get in the first row? (hint: replaces)
I'm almost a complete beginner with MATLAB with no formal training so please forgive me for not understanding.
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.
Also, the statement
else
H(rows,cols) = V(rows); % Remainder of cells
end
takes the value of (rows) at the current matrix element and applies V(rows) to the cell without overwriting preceeding values. Can you see where the confusion is coming from?
I can't see a more efficient way of only applying an equation to only the 1st row and 1st column.
Thank you for responding, hope you can help further!
"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?
Oh, huh, it was that simple. Thanks very much!
Edit, this is the equation I'm trying to fit to the matrix:

Sign in to comment.

More Answers (1)

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.

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!