Insert a vector into a matrix without where the row placement is given from a number
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Torsten
2019-8-6
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.
采纳的回答
neil jerome
2019-8-6
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 个)
neil jerome
2019-8-7
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 个评论
neil jerome
2019-8-7
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle & Nuclear Physics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!