Add zeros to matrices same as length of another matrix

26 ビュー (過去 30 日間)
Becky CNS
Becky CNS 2018 年 3 月 15 日
コメント済み: KL 2018 年 3 月 15 日
I have to determine the beta weights of an fmri data subset. To do this I think I should perform a multiple regression of the bold signal of the voxels with the Design matrix (convolved with the hrf) - please correct me if I'm wrong in this.
The problem is the bold values are in a 360x3 matrix and the Design is 369x5. This is probably a very long way about it but to be able to regress, I separated each bold voxel into a separate vector and tried to pad the end with zeros which gives me an error everytime. How do I change this code to work for matrices? Or can I add zeros to the bold 360x3 matrix to make it 369x3 in one go?
bold1=[bold1, zeros(1,length(X(:,1))-length(bold1))];
  2 件のコメント
Jos (10584)
Jos (10584) 2018 年 3 月 15 日
You will get better answers if you are more precise in describing the exact error. " which gives me an error everytime " is very vague ...
Becky CNS
Becky CNS 2018 年 3 月 15 日
Usually this one. 'Error using horzcat Dimensions of matrices being concatenated are not consistent.'
Or 'Error using vertcat Dimensions of matrices being concatenated are not consistent.'

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

採用された回答

KL
KL 2018 年 3 月 15 日
編集済み: KL 2018 年 3 月 15 日
can I add zeros to the bold 360x3 matrix to make it 369x3 in one go?
You say you want to add 9 rows but you are trying to add columns with your code. Probably something like,
bold1=[bold1; zeros(size(X(:,1),1)-size(bold1,1),3)];
...Dimensions of matrices being concatenated are not consistent...
This simply means what it says. For example,
>> A = [1 2;3 4]
A =
1 2
3 4
>> B = [5 6 7; 8 9 0]
B =
5 6 7
8 9 0
>> C = [A; B]
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
You cannot stack them up and down (vertical cancatenation, hence vertcat) because they have different number of columns (hence not consistent!).
But if you want to stack them side by side (horizontal)
>> C = [A B]
C =
1 2 5 6 7
3 4 8 9 0
Now you can, because they have same number of rows. Simple!
  5 件のコメント
Steven Lord
Steven Lord 2018 年 3 月 15 日
You can simplify this:
size(X(:,1),1)
X(:, 1) has the same number of rows as X and 1 column. So size(X(:, 1), 1) returns the number of rows in X. Why go to the trouble of extracting that first column? Just ask for the size of X in the first dimension.
size(X, 1)
KL
KL 2018 年 3 月 15 日
I agree!

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

その他の回答 (2 件)

ES
ES 2018 年 3 月 15 日
bold1 = [bold1, zeros(360,2);zeros(9,5)]
  1 件のコメント
Becky CNS
Becky CNS 2018 年 3 月 15 日
That gives me an 'Error using vertcat'?

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


Jos (10584)
Jos (10584) 2018 年 3 月 15 日
To pad an matrix A with zeros to match a larger or same-sized array B, you can use this:
A = magic(3)
B = ones(3, 5)
newA = zeros(size(B))
newA(1:size(A,1), 1:size(A,2)) = A
If you are sure the new dimensions are larger, this will also work:
A2 = magic(4)
B = ones(5,7) % all dimensions larger than A
A2(size(B,1), size(B,2)) = 0

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by