how can I add and change the position of elements in vector
4 views (last 30 days)
dpb on 10 May 2022
Edited: dpb on 10 May 2022
NB: even if you have (again the unstated problem)
>> ma=[repmat(ma(3),2,1); sum(ma(1:2))]
the above solution works from the base definition of ma with the components a, b.
Of course, if you replace ma with the new version by executing the last line a second time, the result will then be some new matrix with different characteristics; it's operation-order dependent when you do that. If you must preserve the original ma, then you'll have to create mb instead of overwriting ma as you've illustrated so far. IOW, you MUST keep the code snippet above together if the result is to be as expected.
The key thing here is that MATLAB evaluates the RHS of an assignment statement in its entirety with values of all variables in context at the time of execution of the given assignment statement, including the present content of whatever is the LHS variable (if referenced as is here, of course). Hence, all the elements of ma are those at the end of the previously executed statement and are not affected by anything done on the RHS until they completely replace the previous assignment. Any assignment to a variable on the LHS without explicit indexing completely clears and replaces the entire variable as if it had never existed up to that time.