How to create a vector with vaules based on a if statement
3 次查看(过去 30 天)
显示 更早的评论
I have a task where I need to calculate the surface resistivity of a foundation pile.
The surface resistivity differs based on soil type: clay or sand. For my task I have 4 soil layers where the first two are clay and the two last are sand.
To set up the layer type I've created a vector as follows:
L = [1;1;2;2]; % Layer type: 1=clay | 2=sand
The surface resistivity in a layer is calculated as follows:
If the layer type is clay:
Rs = alpha.*cu.*As % Where alpha is a scalar and cu, As are a 4x1 vector with layer properties
If the layer type is sand:
Rs = Nm.*qs.*As % Where Nm is a scalar and qs, As are a 4x1 vector with layer properties
I could then calculate each layer separately, but to make my script more convenient I'd like to use the two above formulas to calculate Rs as a 4x1 vector showing the surface resistivity in each layer. Here so that the two first values of Rs is calculated using the first formula for clay and the 2 last values using the second formula for sand.
I'm pretty new to matlab, so my skills are fairly basic. So far I've tried something like this,
if L==1;
Rs=alpha.*cu.*As
elseif L==2
Rs=qs.*Nm.*As
end
But this doesn't seems to work and doesn't actually give any output for Rs nor any error.
Edit: The result I'm expecting and trying to achieve is as follow
alpha = 0.4;
Nm = 0.6;
qs = [25;75;125;175];
As = [5;5;5;5]:
cu = [100;100;0;0];
Rs = [200;200;375;525]
Hope it's clear for what I'm trying to do.
0 个评论
采纳的回答
Star Strider
2017-11-23
One option:
L = [1;1;2;2]; % Layer type: 1=clay | 2=sand
alpha = rand;
Nm = rand;
qs = rand(4,1);
As = rand(4,1);
cu = rand(4,1);
Rsfcn = @(L) [(alpha*cu.*As)'.*(any((L==1),2)), (Nm*qs.*As)'.*(any((L==2),2))]';
Rsm = Rsfcn(L);
for k1 = 1:length(L)
Rs(:,k1) = Rsm(Rsm(:,k1) ~= 0,k1);
end
This uses an anonymous function to do the calculations, and a for loop to strip the zero values from the columns, creating as ‘Rs’ a (4 x length(L)) array, where the columns correspond to the elements in ‘L’. (This assumes that all calculated values are not equal to zero.)
3 个评论
Star Strider
2017-11-23
编辑:Star Strider
2017-11-24
Change the ‘for’ loop to:
for k1 = 1:length(L)
Rs(:,k1) = Rsm((1:4)+4*(L(k1)-1),k1);
end
That should do what you want. Again, ‘Rs’ are columns respectively corresponding to the values of ‘L’.
Note that the code you posted creates (4x1) vectors for ‘Rs’ for each value of ‘L’. I combined those vectors to create them as individual column vectors in the ‘Rs’ matrix.
——————————
EDIT —
I do not understand mathematically how you get the values for ‘Rs’ that you posted, or how to generalise it to other values of ‘L’. I leave that to you.
That aside, this version of the for loop will produce the ‘Rs’ vector you posted as your desired solution:
for k1 = 1:length(L)
M(:,k1) = Rsm((1:4)+4*(L(k1)-1),k1);
Rs(k1,:) = M(k1,k1);
end
更多回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!