horzcat different size rows; zero padding

17 ビュー (過去 30 日間)
Jennifer
Jennifer 2011 年 5 月 27 日
編集済み: Jan Siegmund 2020 年 5 月 18 日
Hi,
I have 6 matrices; all are 7 columns. The number of rows is 12, 14, 16, 17, 17, 17. I want to pick off the first row from each and put them into one matrix.
I create a matrix of zeros that is the final size I need of (17,7), but when I run the following I get a 'CAT arguments dimensions are not consistent'.
A = zeros(17,7);
A = [B(:,1) C(:,1) D(:,1) E(:,1) F(:,1) G(:,1)];
The problem is that the first row does not keep the trailing zeros after the 12 cells are filled with the data. I don't understand why this happens. How can I concatenate these rows?
Thank you!
  1 件のコメント
Oleg Komarov
Oleg Komarov 2011 年 5 月 27 日
Not clear what are you trying to do.
Post the result of "whos" with B-G only in the workspace.

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

採用された回答

Jan
Jan 2011 年 5 月 27 日
Preallocating A is useless here, because the 2nd line does not write into A, but overwrites A:
A = zeros(17,7); % Useless
A = [B(:,1) C(:,1) D(:,1) E(:,1) F(:,1) G(:,1)]; % Error!
Matt's solution works fine, but is not comfortable. Have you seen FEX: PADCAT? It pads the vectors with NaN's but they can replaced easily afterwards:
A(isnan(A)) = 0;
  1 件のコメント
Jennifer
Jennifer 2011 年 10 月 19 日
Wow sorry I didn't reply and accept an answer! This is what I ended up doing....thanks!!

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

その他の回答 (2 件)

Matt Fig
Matt Fig 2011 年 5 月 27 日
A = [1,2,3,4].';
B = [1 2 3 4 5 6].';
C = zeros(6,2);
C(1:length(A),1)= A % Fill first column with A
C(1:length(B),2)= B % Fill second column with B

Jan Siegmund
Jan Siegmund 2020 年 5 月 18 日
編集済み: Jan Siegmund 2020 年 5 月 18 日
If the input data is a cell array of column vectors, you might consider this:
a = {ones(5,1) ones(8,1)}; %test data
len = max(cellfun('length',a));
b = cellfun(@(x)[x;zeros(len-size(x,1),1)],a,'UniformOutput',false);
out = [b{:}]

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by