MATLAB Answers

Take values from an matrix automatically.

1 view (last 30 days)
Ricardo Gutierrez
Ricardo Gutierrez on 10 Sep 2019
Commented: Ricardo Gutierrez on 10 Sep 2019
Hi !!!, Good morning.
I would like to help me in the following:
I have an 18 X 3 matriaz, which can also be 18 X n
Each element of a line is a variable
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc; clear; close all; format short
Prog=[1.19 1.89 2.74 3.53 4.43 1.76 0.22 4.20 2.39 3.97 3.24 4.43 0.96 6.41 2.00 7.07 3.34 6.77
0.56 1.41 2.27 3.23 4.18 1.36 0.96 4.05 1.64 3.66 3.61 3.67 0.53 7.02 2.02 6.30 3.77 6.92
1.13 2.16 2.73 3.87 5.08 1.49 0.46 3.80 1.65 4.13 3.62 4.09 0.69 6.86 2.14 6.30 3.31 7.03 ];
% To get each of the elements in a variable I do the following for the first line.
dx1=Prog(1,1);
dy1=Prog(1,2);
dx2=Prog(1,3);
dy12=Prog(1,4);
dx3=Prog(1,5);
dy13=Prog(1,6);
dx12=Prog(1,7);
dy2=Prog(1,7);
dx21=Prog(1,7);
dy21=Prog(1,10);
dx31=Prog(1,11);
dy22=Prog(1,12);
dx13=Prog(1,13);
dy3=Prog(1,14);
dx22=Prog(1,15);
dy31=Prog(1,16);
dx32=Prog(1,17);
dy32=Prog(1,18);
% With this I have each of the values of the first line.
% How do I automatically get the code to take the values of the following lines?

  5 Comments

Show 2 older comments
Bob Nbob
Bob Nbob on 10 Sep 2019
I think Ted's question is still relevant. I understand that you want to store the matrix information for use outside of a loop, but why not just keep the entire 2D matrix as a 'sheet' in a 3D matrix? I would offer an example, but I have no real idea how your process is organized, so I'm not sure how to make something relevant. The only thing I would say is that you can index the different values you want, instead of creating a variable for each value.
Prog(1,1,i); % dx1 where i is the iteration number you want
Also, before somebody else mentions it, it is generally considered bad practice to generate a lot of variables with sequential naming (i.e. dx1, dx2, dx3, ...). MATLAB is designed to work with values in matrices, so it is always best to try and keep matrices together.
Walter Roberson
Walter Roberson on 10 Sep 2019
If you need to take a copy of the current values to use in calculations while you change the array, then you can
saved_row = Prog(i,:);
and then saved_row(6) would be dy13 and so on without having to write each value to a variable individually.

Sign in to comment.

Answers (1)

Fabio Freschi
Fabio Freschi on 10 Sep 2019
Prog_old = Prog;
% modify Prog as desired
Prog = Prog*2; % dummy operation
% reload the old version
Prog = Prog_old;

Translated by