how to insert the column [60;80] into the third column

18 ビュー (過去 30 日間)
Bella
Bella 2016 年 5 月 18 日
編集済み: Jos (10584) 2016 年 5 月 19 日
A=[1 5 6; 3 0 8]

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 5 月 18 日
編集済み: Azzi Abdelmalek 2016 年 5 月 18 日
A=[1 5 6; 3 0 8]
A=[A [60;80]]
%Or
A(:,end)=[60;80]
  1 件のコメント
Image Analyst
Image Analyst 2016 年 5 月 18 日
This appends. It does not insert. The data are not even in column 3 - they're in column 4. Even though my code is longer because of comments and error checking, I think it's the most robust, and it will insert or append depending on how many columns there are already.

サインインしてコメントする。

その他の回答 (4 件)

Image Analyst
Image Analyst 2016 年 5 月 18 日
Bella, here's a fairly robust way:
% Define sample data.
A=[1 5 6; 3 0 8]
[rows, columns] = size(A);
% Define a column vector of data you'd like to insert.
colVecToInsert = [60;80]
% Define the column you want to insert it to
col = 3;
% Do the insert
if col >= 1 && col <= (columns + 1)
A = [A(:,1:col-1), colVecToInsert, A(:, col:end)]
elseif col > (columns + 1)
% Will append to right side.
A = [A, colVecToInsert]
else
warningMessage = sprintf('Cannot insert into column %d', col);
uiwait(warndlg(warningMessage));
end
  1 件のコメント
Ahmet Cecen
Ahmet Cecen 2016 年 5 月 18 日
Ooh I see what the question meant now.

サインインしてコメントする。


Ahmet Cecen
Ahmet Cecen 2016 年 5 月 18 日
A(:,3) = A(:,3) + [60;80];
  3 件のコメント
Bella
Bella 2016 年 5 月 18 日
figured it out, thanks
Ahmet Cecen
Ahmet Cecen 2016 年 5 月 18 日
This is putting it back into that column.

サインインしてコメントする。


Jos (10584)
Jos (10584) 2016 年 5 月 18 日
編集済み: Jos (10584) 2016 年 5 月 18 日
A = [1 5 6 ; 0 3 8]
A(:,3) = [60 ; 80] % insert a vector in the 3rd column
  2 件のコメント
Image Analyst
Image Analyst 2016 年 5 月 18 日
This overwrites, not inserts.
Jos (10584)
Jos (10584) 2016 年 5 月 19 日
編集済み: Jos (10584) 2016 年 5 月 19 日
True, but isn't there a difference between "insert into" and "insert before"/"insert after"? Confusing question → different answers.

サインインしてコメントする。


Jos (10584)
Jos (10584) 2016 年 5 月 19 日
For what it is worth, take a look at INSERTROWS, which you can use with transpose to get "insertcolumns" after (or before) a specific column
A = [1 5 6 ; 3 0 8]
B = [60 ; 80]
ColumnToInsertB_before = 3
C = insertrows(A.', B.', ColumnToInsertB_before-1).'
The function INSERTROWS can be downloaded via this link: http://blogs.mathworks.com/pick/2008/05/16/inserting-rows/

カテゴリ

Help Center および File ExchangeDialog Boxes についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by