Faster way of replacing multiple rows with same vector without using a loop?
显示 更早的评论
Hi,
I have a matrix a (see below). I would like to replace the 1st and 3rd row with the same row vector [255 100 0].
a =
245 255 255
254 252 255
251 250 239
Initially I created an index variable 'idx'
idx = [1;3]; % indexing row number for vector replacement
However
a(idx,:) = [255 100 0 ];
would give error messages
'Unable to perform assignment because the size of the left side is 2-by-3 and the size of the right side is 1-by-3'.
How to do it correctly without do it in a loop?
采纳的回答
更多回答(2 个)
madhan ravi
2019-1-29
idx = [1 3];
a(idx,:) = 0
5 个评论
madhan ravi
2019-1-29
编辑:madhan ravi
2019-1-29
a(idx,:) = [255 100 0]
Stephen23
2019-1-29
madhan ravi
2019-1-29
Ah you are right Stephen I was careless when posting it thnx though!
I don't think it's worth avoiding a loop here. A loop over columns, because there are so few of them, would be as fast as pretty much whatever else you might do, no matter how many rows a has.
rhs=[255,100,0];
for j=1:3
a(idx,j)=rhs(j);
end
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!