Create a matrix depending on values in another one

1 Ansicht (letzte 30 Tage)
Daniel Arvidsson
Daniel Arvidsson am 23 Apr. 2017
Bearbeitet: Stephen23 am 23 Apr. 2017
Hello!
I have the follwoing code and the result i want is in the end. So, im coming in the for loop and ask if a certain value exist. If then that exist i want to assign crit(:,i)=crit_1 but here is the thing; I want also crit(:,i+1)=crit_1 but the problem now is when im coming to the top of the for loop again and i=2, my code replaces column number two with my new condition which i dont want. Now i bascially want to start at i=3 so it replaces the two next columns. Hope this makes sense, thank you!
% Testing
clc
Lam_1=[40 9.8 2.8 0.3]; Lam_2=[210 20 6 0.3]; Lam_3=[136 10 5.2 0.3]; Lam_4=[151 9.4 4.8 0.31]; % Materials [E1 E2 G12 v12] Lam_5=[75 6 2 0.34]; Lam_6=[147 9 3.3 0.31]; Lam_7=[181 10.3 7.17 0.28];
crit_1=[1100 20 600 140 70]'; crit_2=[1400 80 2800 280 120]'; crit_3=[1800 40 1200 220 80]'; crit_4=[2260 50 1200 190 100]'; crit_5=[1400 30 280 140 60]'; crit_6=[2260 50 1200 190 100]'; crit_7=[1500 40 1500 246 68]';
Layup=[Lam_1 0 0.127; Lam_2 pi/2 0.127; Lam_3 0 0.127]; % Can change rowsizes
sigma_local =ones(3,6); % This one contains a bunch of other values usaually
crit=zeros(5,size(sigma_local,2));
for i=1:size(Layup,1)
if Layup(i,1) == 40
crit(1:5,i)=crit_1;
end
if Layup(i,1) == 210
crit(1:5,i)=crit_2;
end
if Layup(i,1) == 136
crit(1:5,i)=crit_3;
end
if Layup(i,1) == 151
crit(1:5,i)=crit_4;
end
if Layup(i,1) == 75
crit(1:5,i)=crit_5;
end
if Layup(i,1) == 147
crit(1:5,i)=crit_6;
end
if Layup(i,1) == 181
crit(1:5,i)=crit_7;
end
end
% Below is the desired output matrix i want, hence this one
crit=[crit_1 crit_1 crit_2 crit_2 crit_3 crit_3];
  3 Kommentare
Daniel Arvidsson
Daniel Arvidsson am 23 Apr. 2017
Hello!
No, ur right, sorry. Crit is the desired output, i just made it myself, crit=[crit_1 crit_1 crit_2 crit_2 crit_3 crit_3];
sigma_local=ones(3,6)
Daniel Arvidsson
Daniel Arvidsson am 23 Apr. 2017
I also edited my question.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Stephen23
Stephen23 am 23 Apr. 2017
Bearbeitet: Stephen23 am 23 Apr. 2017
Your code does not make good use of MATLAB. In particular you make your own life much harder by splitting the data up into lots of separate variables, and trying to process those variables using loops and if-s. This makes for large, pointlessly complicated code, which in turn will be pointlessly buggy and hard to debug (because of its complexity). And by creating numbered variables you end up putting meta-data into variable names, which is always a bad idea.
You really need to be using indexing, not numbered variables.
The name MATLAB come from "MATrix LABoratory", and not from "lets split the data up into lots of separate variables and make them harder to work with". When you learn to work with matrices (and other arrays), then you will learn how to use MATLAB efficiently. For example, when I put all of your numeric data into three simple numeric arrays, then your task become trivial with just one command and some indexing:
% [E1, E2, G12,v12] Materials
Lam = [...
40, 9.8, 2.8,0.3;...
210, 20, 6,0.3;...
136, 10, 5.2,0.3;...
151, 9.4, 4.8,0.31;...
75, 6, 2,0.34;...
147, 9, 3.3,0.31;...
181,10.3,7.17,0.28];
crit = [...
1100,20, 600,140, 70;...
1400,80,2800,280,120;...
1800,40,1200,220, 80;...
2260,50,1200,190,100;...
1400,30, 280,140, 60;...
2260,50,1200,190,100;...
1500,40,1500,246, 68];
Layup = [...
Lam(1,:), 0,0.127;...
Lam(2,:),pi/2,0.127;...
Lam(3,:), 0,0.127];
[~,idx] = ismember(Layup(:,1),Lam(:,1));
out = crit(idx,:)
produces exactly the required output, picking the rows of crit based on the first column of Layup:
out =
1100 20 600 140 70
1400 80 2800 280 120
1800 40 1200 220 80
Easy, once you keep your data together in matrices. In general you should store data in the simplest array possible. When you do this then your code will be correspondingly simpler. And simpler code is less buggy.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by