Asked by Salar
on 30 May 2014

Hello, let's say I have a matrix A = [1+x,1;2,5] so I want to have x to go from -20 to 20 with x~=0

then I would need to have A(i) matrices with i=40 . How do I do this on Matlab? I've been reading on internet on indexing and things like that, but I keep getting errors. I really appreciate you taking the time.

Sincerely,

Answer by Image Analyst
on 30 May 2014

Accepted Answer

I show you two different methods in the loop below:

counter = 1;

A3D = [];

for x = -20 : 20

if x == 0

continue;

end

% Method #1: Load into a cell array.

caA{counter} = [1+x,1;2,5];

% Method #2: Load into a 3D regular, normal array

A3D = cat(3, A3D, [1+x,1;2,5]);

counter = counter + 1;

end

% Print to command window

celldisp(caA)

A3D

Finally I refer you to the FAQ http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Mahdi (view profile)

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/131800-how-do-i-add-a-value-in-one-element-of-a-matrix-in-a-for-loop#comment_216896

## Salar (view profile)

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/131800-how-do-i-add-a-value-in-one-element-of-a-matrix-in-a-for-loop#comment_216925

Sign in to comment.