Building a matrix in a faster way
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi,
I am trying to build a matrix by giving each array in the matrix the same value in its first column. The value is [0;0;1]. My code look something like this:
yv = 1:-1:-1;
xv = -1:1:1;
for Y = 1:length(yv)
for X = 1:length(xv)
M(:,1,X,Y) = [0;0;1];
end
end
I was wondering if there is more efficient way to give the arrays for length (yv) and (xv) the value [0;0;1] instantly without using the for loop. My matrix in original is much larger than this and I need to make the code as faster to execute the data as possible.
Highly appreciate any help with this.
Best wishes
AA
0 Kommentare
Akzeptierte Antwort
Matt J
am 16 Okt. 2012
d=[0;0;1];
M=d(:,1,ones(1,length(xv)), ones(1,length(yv)))
2 Kommentare
Walter Roberson
am 16 Okt. 2012
Which can also be written as
M = repmat(d, [1, 1, length(xv), length(yv)]);
Matt J
am 16 Okt. 2012
Yes, although repmat does use mcode containing loops, and therefore can be slow.
Siehe auch
Kategorien
Mehr zu Matrix Indexing 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!