Replace portion of array with another array of different size
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Nathaniel H Werner
il 21 Set 2018
Modificato: Stephen23
il 21 Set 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?
0 Commenti
Risposta accettata
Stephen23
il 21 Set 2018
Modificato: Stephen23
il 21 Set 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)]
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!