How to create a vector with vaules based on a if statement

3 vues (au cours des 30 derniers jours)
Marius Hammer
Marius Hammer le 23 Nov 2017
Modifié(e) : Star Strider le 24 Nov 2017
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.

Réponse acceptée

Star Strider
Star Strider le 23 Nov 2017
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 commentaires
Star Strider
Star Strider le 23 Nov 2017
Modifié(e) : Star Strider le 24 Nov 2017
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
Marius Hammer
Marius Hammer le 23 Nov 2017
Modifié(e) : Marius Hammer le 24 Nov 2017
Nice thank you, that actually works almost great. Your ‘for’ loop does for me return a 4x4 array showing the result for each valeu of ‘L’, as you seem to describe.
I tried to replace the ‘1:4’ with ‘k1’ and it actually returns a 1x4 array instead of a 4x4 with my expected results. Then transposing it gives me Rs as a 4x1 vector.
for k1 = 1:length(L)
Rs(:,k1) = Rsm((k1)+4*(L(k1)-1),k1);
end
Rs=Rs'
I'm not sure I actually understand how this code works different from yours. With ‘L’ having lenght = 4 would make ‘k1=1:4’ which is the same as you have written for first parentheses, yet yields different output.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 23 Nov 2017
Either use a for loop or use logical indexing

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by