make several matrices in a for loop ERROR

Hi,
When i try to run the next for loop i get: (i display here the variables involved.
Unable to perform assignment because brace indexing is not supported for variables of this type.
Error in A******a (line 41)
k{i}= [E*A/long(i) 0 -E*A/long(i) 0
i would like (in this case to get three similar matrices
Hope someone can help. Thanks in advance
element =
1 2 1
2 3 1
3 4 1
>> 1:size(element,1)
ans =
1 2 3
E = 2040000
A = 4
>> long
long =
5.0000 4.1231 5.0000
for i = 1:size(element,1)
k{i}= [E*A/long(i) 0 -E*A/long(i) 0
0 0 0 0
-E*A/long(i) 0 E*A/long(i) 0
0 0 0 0];
end

Réponses (2)

Image Analyst
Image Analyst le 4 Fév 2022
This runs without error:
element = [
1 2 1
2 3 1
3 4 1]
[rows, columns] = size(element)
ramp = 1:rows
E = 2040000
A = 4
long = [5.0000, 4.1231, 5.0000]
for ii = 1 : rows
k{ii}= [E*A/long(ii) 0 -E*A/long(ii) 0
0, 0, 0, 0
-E*A/long(ii), 0, E*A/long(ii), 0
0, 0, 0, 0];
end
celldisp(k)
KSSV
KSSV le 4 Fév 2022
Modifié(e) : KSSV le 4 Fév 2022
You have to initialize k as a cell.
element = [ 1 2 1
2 3 1
3 4 1] ;
E = 2040000 ;
long = [5.0000 4.1231 5.0000];
A = 4 ;
k = cell(size(element,1),1) ;
for i = 1:size(element,1)
k{i}= [E*A/long(i) 0 -E*A/long(i) 0
0 0 0 0
-E*A/long(i) 0 E*A/long(i) 0
0 0 0 0];
end
It looks like you have initilized k as a 3D matrix, but you have indexed it as a cell. So you got the error.
I would suggest you to use:
element = [ 1 2 1
2 3 1
3 4 1] ;
E = 2040000 ;
long = [5.0000 4.1231 5.0000];
A = 4 ;
k = zeros(4,4,3) ;
for i = 1:size(element,1)
k(:,:,i)= [E*A/long(i) 0 -E*A/long(i) 0
0 0 0 0
-E*A/long(i) 0 E*A/long(i) 0
0 0 0 0];
end

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by