Addition of matrix in a loop
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have four matrix :
r1=[1 2 3 4 5];
r2=[3 4 5 6 7];
d1=2*r1+5
d2=5*r2-2;
I need to add these two matrix such that result matrix
d=d1+d2
for elements in
r1==r2 (for r1=r2=3,4,5)
and for
r1==[1,2], d=d1
and for
r2==[6,7], d=d2.
Result must get matrix should be 1x 7. I could add these a zeros matrix but want using loop which can be extend to any other matrix as well.
NB: Took the liberty to try to clarify a little as I understood the question--dpb
4 commentaires
dpb
le 31 Déc 2014
Ya' lost me...don't see how the above matches the example's pattern nor quite follow what you're actually trying to do in the latter. Note, however, that intersect may not succeed with floating point comparisons owing to the vagaries of making floating-point tests for equality. The former example works because the vectors are integral values; for floating point for robustness you'll probably need to use a "fuzzy" compare.
Réponse acceptée
dpb
le 30 Déc 2014
First define a couple of useful function handles...
>> f1=@(x) 2*x+5;
>> f2=@(x) 5*x-2;
Then find the intersecting values...
>> xi=intersect(r1,r2);
and then build the output vector...
>> [f1(setdiff(r1,xi)) f1(xi)+f2(xi) f2(setdiff(r2,xi))]
ans =
7 9 24 31 38 28 33
>>
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!