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?

 채택된 답변

Stephen23
Stephen23 2019년 1월 29일
편집: Stephen23 2019년 1월 29일
Here are your two main choices for avoiding a loop.
Method one: match the elements. You need to make the LHS and the RHS have the same number of elements (and they need to be in the required order). Here is one easy way to do that:
a(idx,:) = repmat([255,100,0],numel(idx),1)
Tested:
>> idx = [1;3];
>> vec = [255,100,0];
>> a = [245,255,255;254,252,255;251,250,239]
a =
245 255 255
254 252 255
251 250 239
>> a(idx,:) = repmat(vec,numel(idx),1)
a =
255 100 0
254 252 255
255 100 0
Method two: use scalars. Treat each column individually and assign scalar values. This can be a viable solution if you working with a fixed, small number of columns (e.g. RGB channels):
>> a = [245,255,255;254,252,255;251,250,239]
a =
245 255 255
254 252 255
251 250 239
>> idx = [1;3];
>> a(idx,1) = vec(1);
>> a(idx,2) = vec(2);
>> a(idx,3) = vec(3)
a =
255 100 0
254 252 255
255 100 0
This can also be trivially looped ( I am sure that you can see how).

댓글 수: 3

Thanks Stephen,
I do prefer Method two. But I don't see why it is trivially looped. :)
Matt J
Matt J 2019년 1월 29일
편집: Matt J 2019년 1월 29일
But I don't see why it is trivially looped
Even after having carefully studied my answer?
Stephen23
Stephen23 2019년 1월 29일
편집: Stephen23 2019년 1월 29일
"But I don't see why it is trivially looped. :)"
It isn't looped. I wrote that it can be. Look at the indices: 1, 2, 3.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

idx = [1 3];
a(idx,:) = 0

댓글 수: 5

Salad Box
Salad Box 2019년 1월 29일
편집: Salad Box 2019년 1월 29일
Hi madhan,
Very much appreciated for your prompt solution. :)
[0 0 0] might not be a representitive example in this case. What about if I would like to replace those indexed rows with a vector [255 100 0] instead of [0 0 0]?
P.s. I have also modified my question as I realized that a vector containing three same numbers is more of an extreme case and can be simplified as a single number to solve the problem.
a(idx,:) = [255 100 0]
@madhan ravi: that just takes you back to the same problem in the original question.
Ah you are right Stephen I was careless when posting it thnx though!

댓글을 달려면 로그인하십시오.

Matt J
Matt J 2019년 1월 29일
편집: Matt J 2019년 1월 29일
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

댓글 수: 2

Thanks Matt,
I understand for small number of rows as shown in the example it's worth to do it in a loop, however in real case I have a large number of rows (could be 50k rows for one image multiplying hundreds of images)... so that I was trying to avoid do it in a loop.:)
My loop is over columns!!!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2019년 1월 29일

편집:

2019년 1월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by