Replace portion of array with another array of different size

9 vues (au cours des 30 derniers jours)
Nathaniel H Werner
Nathaniel H Werner le 21 Sep 2018
Modifié(e) : Stephen23 le 21 Sep 2018
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?

Réponse acceptée

Stephen23
Stephen23 le 21 Sep 2018
Modifié(e) : Stephen23 le 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)]

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by