How to overwrite part of a matrix by a function of the row number.
Show older comments
I want to overwrite part of a matrix by a function of the row number.
For example, let
M=rand(10,5) and the function of row number:
f(ROW)=ROW^2.
This function should apply in the range from 1:5 for all columns.
Thus the overwritten matrix N would have the following components
1 1 1 1 1
4 4 4 4 4
9 9 9 9 9
16 16 16 16 16
25 25 25 25 25
0.0620452213196326 0.533771951767000 0.526102465795561 0.337583864052045 0.0513318861123711
0.298243971887764 0.109154212042459 0.729709448223228 0.607865907262946 0.0728852990989761
0.0463512648981808 0.825808857855598 0.707253485315422 0.741254049502218 0.0885274596747204
0.505428142457703 0.338097718802172 0.781377051799277 0.104813241973500 0.798350864113952
0.761425886690113 0.293973053026484 0.287976975614171 0.127888379782995 0.943008139570703
ONLY the initial 5 rows are changed by the power of row number
and ALL columns were modified likewise.
I tried the following approach:
M=rand(10,5);
r=1:1:5;
f=r'.^2;
M(1:5,:)=f;
but did not work.
I hope someone know how to correct this command or to suggest something new.
Thank your for your help
Emerson
Accepted Answer
More Answers (1)
Andrei Bobrov
on 18 Apr 2011
without loop for
r = 1:5;
M(r,:) = r.'.^2*ones(1,size(M,2))
1 Comment
Emerson De Souza
on 18 Apr 2011
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!