Replace portion of array with another array of different size

23 views (last 30 days)
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?

Accepted Answer

Stephen23
Stephen23 on 21 Sep 2018
Edited: Stephen23 on 21 Sep 2018
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)]

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!