Repeating elements in a vector

17 Ansichten (letzte 30 Tage)
Erika
Erika am 10 Okt. 2012
How do I write a function with loops that takes a two column matrix and has the element of one column be repeated by the other column? For example,
A=
4 1
5 2
6 3
>>([4,1; 5,2; 6,3])
ans: 4 5 5 6 6 6
  3 Kommentare
Evan
Evan am 10 Okt. 2012
help repmat
You might find that function to be of use. Also, think about concatenation.
Jan
Jan am 10 Okt. 2012
Please do not post solutions of homework questions. On one hand the OP cannot submit this without cheating, on the other hand his teacher will not recommend this forum anymore. So the OP's and the forum's reputation suffer.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Andrei Bobrov
Andrei Bobrov am 10 Okt. 2012
Bearbeitet: Andrei Bobrov am 11 Okt. 2012
for solution your problems you can use functions of MATLAB: cumsum; zeros.
  2 Kommentare
Jan
Jan am 10 Okt. 2012
Please do not post solutions of homewokr questions. On one hand the OP cannot submit this without cheating, on the other hand his teacher will not recommend this forum anymore. So the OP's and the forum's reputation suffer.
Biside this, your soultion is smart and efficient. +1
Andrei Bobrov
Andrei Bobrov am 11 Okt. 2012
Jan! OK.

Melden Sie sich an, um zu kommentieren.


Sean de Wolski
Sean de Wolski am 10 Okt. 2012
  1 Kommentar
Jan
Jan am 10 Okt. 2012
+1: Up-voted for not posting a solution.

Melden Sie sich an, um zu kommentieren.


Evan
Evan am 10 Okt. 2012
function v = fname(A)
v = [];
for i = 1:size(A,1)
v = [v repmat(A(i,1),1,A(i,2))];
end
  3 Kommentare
Erika
Erika am 11 Okt. 2012
Bearbeitet: Erika am 11 Okt. 2012
I'm not trying to look for answers. I only wanted to know what embedded functions in MATLAB I can use because that's how I'll learn... I guess I should've phrased my question in a better way.
Jan
Jan am 12 Okt. 2012
@Erika: Question, especially from beginners, are very welcome in this forum. Asking a perfect question concerning homework problems is nearly impossible, because this requires a deep understanding of the question, which you reach usually after the problem is solved.
As long as the contributors of this forum answer homework question such general that the pupils/students perform the main part by their own, all kind of questions are welcome and useful.

Melden Sie sich an, um zu kommentieren.

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!

Translated by