# How to apply a equation to specific rows in a matrix

2 vues (au cours des 30 derniers jours)
Philippe Corner le 28 Déc 2018
Commenté : Stephen23 le 28 Déc 2018
If I have a matrix m like:
m=[0 340.216815000000
1.56250000000000 570.718050000000
4.68750000000000 769.256473000000
7.81250000000000 1176.42951000000
10.9375000000000 1632.88855600000
18.4375000000000 2099.99990500000
25.9375000000000 2099.99990500000
33.4375000000000 2099.99990500000
40.9375000000000 2099.99990500000
48.4375000000000 2099.99990500000
0 331.115127000000
1.53698699999995 510.419428000000
4.61084000000005 719.346166000000
7.68469200000004 1145.58672900000
10.7585449999999 1583.81915100000
18.1358640000001 2099.99990500000
25.5130620000000 2099.99990500000
32.8903809999999 2099.99990500000
40.2675780000000 2099.99990500000
47.6448969999999 2099.99990500000];
and a matrix n like:
n=[0 159.846351000000
1.52099599999997 343.757153000000
4.56286599999999 412.372440000000
7.60473600000000 926.872313000000
10.6466060000000 1063.47274800000
17.9471440000000 1079.82945400000
25.2475589999999 1200.00004800000
32.5480960000000 1200.00004800000
39.8486330000001 1200.00004800000
47.1491699999999 1200.00004800000
0 186.247796000000
1.49536100000000 386.090606000000
4.48608399999989 477.258742000000
7.47680700000001 775.100648000000
10.4676509999999 1004.91941000000
17.6453859999999 1075.44088400000
24.8232419999999 1200.00004800000
32.0009769999999 1200.00004800000
39.1787110000000 1200.00004800000
46.3565670000000 1200.00004800000];
how to build a vector E which matches that when m(:,1) is lower than 10, then use this formula:
1. (m(:,2).^2).*(3*(n(:,2).^2));
and when 10 < m(:,1) <= 25, use:
2. (m(:,2).*2)
the results would be this:
E = [8872341649.18581
115469679048.928
301885906398.286
3566916094097.57
3265.77711200000
4199.99981000000
4199.99981000000
4199.99981000000
4199.99981000000
4199.99981000000
662.230254000000
1020.83885600000
1438.69233200000
2291.17345800000
3167.63830200000
4199.99981000000
4199.99981000000
4199.99981000000
4199.99981000000
4199.99981000000];
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Stephen23 le 28 Déc 2018
Modifié(e) : Stephen23 le 28 Déc 2018
Your example output does not match your description: rows 7, 8, 9, 10, 17, 18, 19, and 20 of m do not match either of the conditions that you describe, and yet you show data in those rows of the output matrix. Please explain how that data should be generated.

Connectez-vous pour commenter.

### Réponse acceptée

Stephen23 le 28 Déc 2018
Modifié(e) : Stephen23 le 28 Déc 2018
X = m(:,1)<=10;
Y = 10<m(:,1) & m(:,1)<=25;
Z = nan(size(m,1),1);
Z(X) = m(X,2).^2 .* 3.*n(X,2).^2;
Z(Y) = m(Y,2).*2;
Giving:
>> Z
Z =
8872341649.185812
115469679048.9282
301885906398.2859
3566916094097.571
3265.777112
4199.99981
NaN
NaN
NaN
NaN
11409367861.74257
116507562823.7022
353594015029.8446
2365339054254.324
3167.638302
4199.99981
NaN
NaN
NaN
NaN
##### 4 commentairesAfficher 2 commentaires plus anciensMasquer 2 commentaires plus anciens
Philippe Corner le 28 Déc 2018
Sorry you are right, they should be filled with (m(:,2).*2)
Stephen23 le 28 Déc 2018
X = m(:,1)<=10;
Z = m(:,2).*2;
Z(X) = m(X,2).^2 .* 3.*n(X,2).^2;
Giving:
>> Z
Z =
8872341649.185812
115469679048.9282
301885906398.2859
3566916094097.571
3265.777112
4199.99981
4199.99981
4199.99981
4199.99981
4199.99981
11409367861.74257
116507562823.7022
353594015029.8446
2365339054254.324
3167.638302
4199.99981
4199.99981
4199.99981
4199.99981
4199.99981

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Creating and Concatenating Matrices 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!

Translated by