How to arrange the values of the matrix according to index number?
2 次查看(过去 30 天)
显示 更早的评论
I have index number of matrix A =[1 3 5 6], B= [ 2 4]. The values corresponding to index number matrix A is A1=[3 4 -7 5; 2 5 10 -9; -11 56 9 -4; 6 -7 4 20] and the values corresponding to index number matrix B is B1=[13 4 -70 51; -1 95 12 -9].
I want to generate a matrix C with index number of A and B after sorting like C=[ 1 2 3 4 5 6] and the values of the matrix C1 will arrange according to the index numb of C.
How will be arranging the values of C1 from A1 and B1 according to the index number C? Please help on this
7 个评论
采纳的回答
madhan ravi
2020-9-28
编辑:madhan ravi
2020-9-28
C_new = zeros(numel(A), numel([A, B]));
C_new(:, A) = A1;
C_new(:, B) = B1.'
0 个评论
更多回答(1 个)
madhan ravi
2020-9-30
A faster version of the other answer:
C_new = zeros(numel(A), numel([A, B]));
[IX, ix] = sort([A, B]);
C_new(:, IX) = [A1, B1.'];
C_new = C_new(:, ix)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!