Insert a vector into a matrix without where the row placement is given from a number

3 vues (au cours des 30 derniers jours)
Hi
I have this starting matrix that shows different layers into the soil.
A_example_1 = [0 -0.1; -0.1 -4.7; -4.7 -5];
I need a code that decides where to add my vector into my matrix. Ive defined that:
% Add layer in given depth for both example 1 and 2. In this code the example 1 i chosen.
vector_add = A_example_1(end,2)+1.15;
Now if the matrix is as A is right now, the program should add a layer so it gives this matrix:
ans_example_1 = [0 -0.1; -0.1 -3.85; -3.85 -4.7; -4.7 -5];
However... If the A matrix starts by having these layers:
A_example_2 = [0 -0.1; -0.1 -3; -3 -5];
Then the code should create a matrix that looks like this:
ans_example_2 = [0 -0.1; -0.1 -3; -3 -3.85; -3.85 -5]
  2 commentaires
Torsten
Torsten le 6 Août 2019
Your vector_add is a scalar, namely -5 - 1.15 = -6.15.
So I don't know how you come up with your ans_example_1 and ans_example_2.

Connectez-vous pour commenter.

Réponse acceptée

neil jerome
neil jerome le 6 Août 2019
yeah, the vector_add formulation is odd, and the direction is worng (?), but i think i see what you mean. you actually just have a list of boundary points for the layers - why not add the new boundary point to a vector of these points, then run a couple of lines to sort these and then create the matrix?
% use vector list of layer boundary positions
oldBoundList = [0 -0.1 -4.7 -5];
newBound = oldBoundList(3) + 1.15; % or just 'newBound = -3.55;' etc
newBoundList = sort(horzcat(oldBoundList, newBound), 'descend');
% write matrix
nLayers = length(newBoundList)-1;
mat = NaN(nLayers,2);
for i = 1:nLayers
mat(i,:) = [newBoundList(i) newBoundList(i+1)];
end
this should give you what you want.
  1 commentaire
Mikkel
Mikkel le 6 Août 2019
Modifié(e) : Mikkel le 6 Août 2019
Sorry for the bad formulation, but you did indeed understand what I was looking for :) thank you! I just have one more question
If my matrix had another column with some numbers that corrosond to the given layer, how do I make sure they get arranged correctly and get copied to the new layer that had been added into the matrix.
As an example.
%My old matrix
A_example_1 = [0 -0.1 17; -0.1 -4.7 18; -4.7 -5 19];
%The depth at which the new layer needs to be added (1.15 meters from the bottom "the last number in the second column")
vector_add = A_example_1(end,2)+1.15;
%How the matrix should look like:
ans_example_1 = [0 -0.1 17; -0.1 -3.55 18; -3.55 -4.7 18; -4.7 -5 19];
Hope you can help me :)
Best Regards Mikkel

Connectez-vous pour commenter.

Plus de réponses (1)

neil jerome
neil jerome le 7 Août 2019
hi mikkel,
the code below assumes the new depth is in the middle of the list somewhere. so this shows how to insert a row by breaking the oldMatrix into pieces, which might have been what you expected for the last answer. but even with more columns of data after the layer depth boundaries, you still notice that the first column is just the second column shifted down to allow a zero at the top, so you can 'leave out' this column from any manipulations and easily add it later based on the (new) second column. this would make things easier? but i have given this below to maybe better match your expectations..
good luck.
n.
oldMatrix = [0 -0.1 17; -0.1 -4.7 18; -4.7 -5 19];
newDepth = oldMatrix(end,2)+1.15; % can be combined with next line
newLine = [newDepth 123]; % include the value for the extra column(s)
insertHere = min(find(oldMatrix(:,2) < newDepth)); % find the insert point
% build new matrix using pieces of old, inserting new half-lines
newMatrix = vertcat( oldMatrix(1:insertHere-1,:), ...
[oldMatrix(insertHere,1) newLine], ...
[newDepth oldMatrix(insertHere,2:end)], ...
oldMatrix(insertHere+1,:) );
  2 commentaires
Mikkel
Mikkel le 7 Août 2019
Hey Neil
The code you have writen isnt very complient when there are different number of layers, I need it also to work with only 1 layer, 2 layers, 4 layers and so on. Since I can't know how many layers there are in the soil I'm investigating.
oldMatrix = [0 -2 17]; %Example with one layer
%Result for oldMatrix with 1 soil layer
newMatrix = [0 -0.85 17; -0.85 -2 17];
oldMatrix = [0 -2 17; -2 -4 18]; %Example with two layer
%Result for oldMatrix with 2 soil layers
oldMatrix = [0 -2 17; -2 -2.85 18; -2.85 -4 18];
Do you have smart way of coding this, because right now I'm thinking of making alot of if-statements.
Best Regards Mikkel.
neil jerome
neil jerome le 7 Août 2019
oldMat = [0 -1 17; -1 -2 18; -2 -3 19; -3 -4 20];
newDepth = -4.5;
newLine = [newDepth 123]; % include the value for the extra column(s)
shortMat = vertcat(oldMat(:,2:end), newLine); % remove first column, append new line
[~, depthOrder] = sort(squash(shortMat(:,1)), 'descend'); % sort based on 2nd column
newShortMat = shortMat(depthOrder,:); % reorder lines based on sorting order
newMat = horzcat( [0 squash(newShortMat(2:end,2))]', newShortMat); % restore first column
should've done it this way earlier; now works regardless where the new line needs to be added. hope you can see why this way is better.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Agriculture 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