indexing multiple values in two arrays with different size
3 次查看(过去 30 天)
显示 更早的评论
I have a simple problem that unfortunately I am failiing so solve (and to find solutions in internet).
Assuming I have 2 matrixes
a = [1 1 1 1 2 2 2 2 2 5 5 5; 0 0 0 0 0 0 0 0 0 0 0 0]';
b = [1 2 5; 11 12 15]';
I would like (without a for loop, that is my current, very slow implementation) to assign in a the corresponding values in the second column of b, when a(x, 1) == b(x,1). Basically the final result should be
a = [1 1 1 1 2 2 2 2 2 5 5 5; 11 11 11 11 12 12 12 12 12 15 15 15]
Thanks a lot for any help
0 个评论
采纳的回答
madhan ravi
2019-2-13
A=sum(a(:,1)==b(:,1).');
a(:,2)=repelem(b(:,2),A).'
1 个评论
madhan ravi
2019-2-13
For version prior to 2016b:
A=sum(bsxfun(@eq,a(:,1),b(:,1).'));
a(:,2)=repelem(b(:,2),A).'
更多回答(1 个)
Pietro
2019-2-14
4 个评论
madhan ravi
2019-2-15
So instead of creating variable A in workspace why not directly implement it ?
a(:,2)=repelem(b(:,2),sum(a(:,1)==b(:,1).')).';
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!