Creating Matrix using two vectors and scalar

Hi guys,
I need your help in solving the following problem: Given a row vector, a column vector and an index create a matrix in the following manner (look at example) without using cell arrays and the implementation should be fast:
Remark: "index" correspond to the row column!
Thanks in advance.

5 Kommentare

Geoff Hayes
Geoff Hayes am 17 Dez. 2015
Jack - is this homework, and if it is, what have you tried so far?
jack
jack am 17 Dez. 2015
it is not a homework, and i have tried to create column vector to concate them to get the desired matrix.
Geoff Hayes
Geoff Hayes am 17 Dez. 2015
Please post/attach your code so that we can see what you have attempted and offer some suggestions on how you can arrive at the correct answer.
jgg
jgg am 17 Dez. 2015
Try looking at the repmat function. I think it should solve your problem.
jack
jack am 18 Dez. 2015
Thank you very much, i have solved it using repmat.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Andrei Bobrov
Andrei Bobrov am 17 Dez. 2015

1 Stimme

function out = fun1(r,c,ii)
out = ones(numel(c),1)*r;
out(:,ii) = c;
end
use
r = [2 7 3 5];
c = [2;7;10];
ii = 2;
out = fun1(r,c,ii);
Kirby Fears
Kirby Fears am 17 Dez. 2015

1 Stimme

jack,
Please read the repmat() documentation to understand its interface and functionality. With repmat, you can stack copies of your row vector into a matrix.
You should be able to determine how many copies of your row vector to stack based on the length() or size() of your column vector.
From there, you just have to insert the column vector into the appropriate column using array indexing .

Kategorien

Gefragt:

am 17 Dez. 2015

Kommentiert:

am 18 Dez. 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by