MATLAB Answers

Adding values in a matrice?

1 view (last 30 days)
Philipp Mueller
Philipp Mueller on 19 Jan 2017
Commented: Stephen on 19 Jan 2017
Hi,
How can i add values (new row -> vector row) at this 6*4 double matrice ? Ok this question sounds stupid....
My matrice:
Plot_Positions = [ 3, 21, 7, 7;
12, 21, 7, 7;
3, 12, 7, 7;
12, 12, 7, 7;
3, 3, 7, 7;
12, 3, 7, 7];
Now i want to add this vectors.. How does it work?
Plot_Position_one= [ 83, 21, 7, 87]
Plot_Position_two= [ 3, 21, 57, 57]
Plot_Position_three= [ 33, 321, 37, 7]
Plot_Position_four= [ 3, 21, 27, 27]
Plot_Position_five= [ 53, 21, 47, 72]
Plot_Position_six= [ 3, 21, 27, 87]
Is this possible like this + a for loop? It should work dynamically...
Plot_Positions = cell(1, 6); %???
Plot_Positions{1}= [ 3, 21, 7, 7]
Plot_Positions{2}= [ 12, 21, 7, 7]
Plot_Positions{3}= [3, 12, 7, 7]
Plot_Positions{4}= [12, 12, 7, 7]
Plot_Positions{5}= [ 3, 3, 7, 7]
Plot_Positions{6}= [ 12, 3, 7, 7]
  1 Comment
Stephen
Stephen on 19 Jan 2017
Creating lots of numbered variables and then trying to process them in a loop is a really bad idea:
As Adam already said: keep all your data in one variable and your code will be simpler, faster, and much more robust. Remember to use the dimensions of ND arrays!

Sign in to comment.

Answers (1)

Adam
Adam on 19 Jan 2017
Edited: Adam on 19 Jan 2017
If you just define your values you want to add in a single 6*4 matrix instead of creating 6 un-necessary variables you can just add them together like any two matrices of equal size.
Don't use cell arrays when you don't need to and don't create lots of numbered variables when you don't need to. It just complicates code. Adding numeric matrices is about the simplest operation in Matlab and it works perfectly if you define the things to add correctly!

Tags

Community Treasure Hunt

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

Start Hunting!