add a column between tow columns
11 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I am newbie with matlab. I have a matrix like:
a = [ 2 5 7 ; 3 6 8]
I would like to add new column between column 1 and 2, So I will have:
a = [2 1 5 7; 3 4 6 8]
Let me ask it more general, I want to add a column between any two columns.
Thanks
0 件のコメント
採用された回答
Sara
2014 年 5 月 29 日
Given the array A and the column vector x, let n be the column after which you want to add x into A:
ncol = size(A,2);
cat(2,A(:,1:n),x,A(:,min(n+1,ncol):end))
3 件のコメント
Sara
2014 年 5 月 29 日
As far as I know, you may add x it at the end of A too, so n+1 would be outside A boundaries. It's just a precaution.
その他の回答 (3 件)
Adam
2014 年 5 月 29 日
Hi, look here http://www.mathworks.com/matlabcentral/answers/1085-inserting-a-column-in-a-matrix-without-deleting-any-column
If you use it often, some function handling it should not be problem to program.
Adam
0 件のコメント
Jos (10584)
2014 年 5 月 29 日
% DATA
A = [1 2 3 ; 4 5 6] % original matrix
x = [8 ; 9] % values to insert
J = 2 % insert x AFTER column J into A
% ENGINE
B = [A x]
[~,i] = sort([1:size(A,2) J])
B = B(:,i)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!