How do I add a value in ONE element of a matrix in a for loop?

1 回表示 (過去 30 日間)
Salar
Salar 2014 年 5 月 30 日
コメント済み: Salar 2014 年 5 月 30 日
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,
  2 件のコメント
Mahdi
Mahdi 2014 年 5 月 30 日
Does the second row (2,5) get repeated every time? Or does it become, let's say (you have 1 to 3)
A=[1, 1;
2,1;
3,1;
2,5]
Can you show an example of what you want the output to be?
Salar
Salar 2014 年 5 月 30 日
Sure, let's say A = [1+x,1;2,5] , and for simplicity let's have x goes from -2 to 2 with and increment of one and x~=0, so output will look like =
A_1=[-1,1;2,5]
A_2=[0,1;2,5]
A_3=[2,1;2,5]
A_4=[3,1;2,5]

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2014 年 5 月 30 日
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

その他の回答 (1 件)

sst
sst 2014 年 5 月 30 日
編集済み: sst 2014 年 5 月 30 日
This should not require a for loop:
x = [-20:-1, 1:20].';
Amatrices = arrayfun(@(X) [1+X, 1; 2, 5], x, 'UniformOutput', false);
  1 件のコメント
Salar
Salar 2014 年 5 月 30 日
That works, but do you know how to do it by a for loop?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by