Replace portion of array with another array of different size
9 次查看(过去 30 天)
显示 更早的评论
I have this grid
A = 0:0.01:1;
And I want to refine the grid but only in a small part where some interesting things happen.
So I made the following.
q = 1/16;
S = 0.25;
tq = A(A>=S-q & A<=S+q);
t = linspace(tq(1),tq(end),75);
I want to replace part of A with t, but the part I want replace is a different length array than t.
A(A>=S-q & A<=S+q) = t
gives the error
In an assignment A(:) = B, the number of elements in A and B must be the same.
How can I work around this?
0 个评论
采纳的回答
Stephen23
2018-9-21
编辑:Stephen23
2018-9-21
You cannot replace part of an array with another array that has a different number of elements (unless the replacement is a scalar). But you can easily use indexing to get the parts of the array that you want to keep, and concatenate them together to create a new array:
q = 1/16;
S = 0.25;
A = 0:0.01:1;
L = find(A>=S-q,1,'first');
R = find(A<=S+q,1,'last');
V = linspace(A(L),A(R),75);
B = [A(1:L-1),V,A(R+1:end)]
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!