How to add certain rows to a 2D matrix?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Antonio
am 16 Jan. 2018
Beantwortet: Stephen23
am 17 Jan. 2018
I have a simple 2D matrix like this:
A = [34 10;
23 10;
64 10];
What I need to do is to find the "max(A(:,1))" then "while A(j,1) < max(A(:,1))" add rows like [A(j,1)+1 10] to the matrix; so I want to eventually get this:
A = [34 10;
35 10;
36 10;
37 10;
.
.
.
62 10;
63 10;
64 10;
.
23 10;
24 10;
25 10;
.
.
.
62 10
63 10
64 10
.
64 10];
I have written the following but it does not work:
for j = 1:size(A,1)
while A(j,1) < max(A(:,1))
A(end+1,:) = [A(j,1)+1 10];
end
end
Any ideas how I could do that?
0 Kommentare
Akzeptierte Antwort
Birdman
am 17 Jan. 2018
A=[34 10;23 10;64 10];
val=max(A(:,1));
for i=1:size(A,1)
B{i,1}=[(A(i,1):val).' (A(i,2)*ones(val-A(i,1)+1,1))];
end
B=cell2mat(B)
0 Kommentare
Weitere Antworten (1)
Stephen23
am 17 Jan. 2018
>> A = [34,10;23,10;64,10];
>> X = max(A(:,1));
>> M = cell2mat(arrayfun(@(n)(n:X).',A(:,1),'uni',0));
>> M(:,2) = 10
M =
34 10
35 10
36 10
37 10
38 10
39 10
40 10
41 10
42 10
43 10
44 10
45 10
46 10
47 10
48 10
49 10
50 10
...
60 10
61 10
62 10
63 10
64 10
64 10
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!