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!