How to store a matrix, A, in variable X, where X is also carrying a counter "i"

I have a matrix A, which I want to store in a varaible X.
Now I can do,
X=A;
But the varaible is X is having a counter i, beacuse the value of X variable in one iteration will be used in the next iteration and so on.
So, it put a counter like this;
X(i)=A;
But, in the above line, the indices error - Unable to perform assignment because the indices on the left side are not compatible with the size of the right side - is shown.
What should be the correct syntax, plaease tell me.

 Akzeptierte Antwort

Ulli Srinivasu
Ulli Srinivasu am 16 Sep. 2020
Hi Neeraj,
simply create a structure.
X = struct();
X(i).A = A;

3 Kommentare

Hi Ulli Srinivasu,
What you said worked. But, now the variable X becomes 'struct", so I can't use it in further equations invloving multiplication of variable X.
Please, see the code from line 27 and the error in command window, in the pic attached below-
use x(j-1).A, x(j).A, x(j+1).A
It worked!! Thanks Ulii Srinivasu!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Simple way: add a third dimension, "pages":
X(:,:,i) = A;
But, if you only need the last iteration, it's better to store just the current and last iteration. Something like:
n=10;
m=4;
X_new = rand(n,m);
for ind=1:10
X_old = X_new;
X_new = X_old.^2;
end

4 Kommentare

I tried the solution you provided-
X(:,:,i) = A;
Say, matrix,
A=[2; 2; 2]
and, i=2, what MATLAB does is, it creates two matrices, shown below, in the result-
x(:,:,1) =
0
0
0
x(:,:,2) =
2
2
2
So, actually i wanted just the second matrix, but it creates one extra matrix (the null matrix) which is undesired and makes the results faulty.
If i=3, it will be create two extra matrices (null matrices). In my program, i is varying upto i=25, so there it will create 24 extra matrices.
So, what should be done now?
What you are seeing displayed is not two matrices, it is a way to represent a multidimensional matrix of numeric values on the command window.
I would suggest that you use SIndar's X_old strategy.
Neeraj Kumar
Neeraj Kumar am 16 Sep. 2020
Bearbeitet: Neeraj Kumar am 16 Sep. 2020
Ok, i will try this X_old strategy. Thanks Walter! Thanks Sindar!

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by