How can I creat a "for" that builds more than one matrix?

1 次查看(过去 30 天)
Basically, I need to creat a Space Truss Member Stiffness Matrix for Member Axes, and every single beam of the truss needs a Stiffness matrix. 'xbar' contain the number of beams in the space truss. The main objective is to get the number of matrix that I need to use the element finite method. But I don't know how to do this at Matlab. I try it earlier this way, but I got the "()-indexing must appear
last in an index
expression. " error.
for i = 1:xbar
rigidlocal(i) = zeros(xbar,xbar);
rigidlocal(i)(1,4) = -1;
rigidlocal(i)(4,1) = rigidlocal(1,4);
rigidlocal(i)(1,1) = 1;
rigidlocal(i)(4,4) = rigidlocal(1,1);
end

回答(1 个)

Anurag Ojha
Anurag Ojha 2024-8-14
Hi Santos
The "()-indexing must appear last in an index expression," occurs because MATLAB does not support indexing with parentheses. Instead, use curly braces {} to index into a cell array.
Here is how the updated code will look like:
xbar = 10; % Number of beams in the space truss
rigidlocal = cell(xbar, 1); % Initialize cell array to store stiffness matrices
for i = 1:xbar
rigidlocal{i} = zeros(4); % Initialize a 4x4 matrix for each beam
rigidlocal{i}(1, 4) = -1;
rigidlocal{i}(4, 1) = rigidlocal{i}(1, 4);
rigidlocal{i}(1, 1) = 1;
rigidlocal{i}(4, 4) = rigidlocal{i}(1, 1);
% Display the matrix
fprintf('Stiffness matrix for beam %d:\n', i);
disp(rigidlocal{i});
end
Stiffness matrix for beam 1:
1 0 0 -1 0 0 0 0 0 0 0 0 -1 0 0 1
Stiffness matrix for beam 2:
1 0 0 -1 0 0 0 0 0 0 0 0 -1 0 0 1
Stiffness matrix for beam 3:
1 0 0 -1 0 0 0 0 0 0 0 0 -1 0 0 1
Stiffness matrix for beam 4:
1 0 0 -1 0 0 0 0 0 0 0 0 -1 0 0 1
Stiffness matrix for beam 5:
1 0 0 -1 0 0 0 0 0 0 0 0 -1 0 0 1
Stiffness matrix for beam 6:
1 0 0 -1 0 0 0 0 0 0 0 0 -1 0 0 1
Stiffness matrix for beam 7:
1 0 0 -1 0 0 0 0 0 0 0 0 -1 0 0 1
Stiffness matrix for beam 8:
1 0 0 -1 0 0 0 0 0 0 0 0 -1 0 0 1
Stiffness matrix for beam 9:
1 0 0 -1 0 0 0 0 0 0 0 0 -1 0 0 1
Stiffness matrix for beam 10:
1 0 0 -1 0 0 0 0 0 0 0 0 -1 0 0 1

类别

Help CenterFile Exchange 中查找有关 Structural Analysis 的更多信息

产品


版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by