Sidharth Prasad
Sidharth Prasad

Reputation: 27

Adding a smaller array to a part of a bigger array?

I have a large array of dimension 64x4x45x14.

I initialize it to all zeros as:

Main = zeros(64,4,45,14);

I have another array S_avg of dimension 45x14 ;

If I do something like this, why does Matlab give an error?

Main(chan_no,level,:,:) = Main(chan_no,level,:,:) + S_avg ;

Upvotes: 1

Views: 119

Answers (2)

Rash
Rash

Reputation: 4336

You can remove the singleton dimensions with squeeze.

Use this code instead,

Main(chan_no,level,:,:) = squeeze(Main(chan_no,level,:,:)) + S_avg ;

The reason is that,

size(Main(chan_no,level,:,:)) = 1   1   45   14

While,

size(S_avg) = 45   14

so you get a dimension mismatch error.

Upvotes: 1

rst
rst

Reputation: 2714

You have to reshape the matrix S_avg first, try this here

Main(chan_no,level,:,:) = Main(chan_no,level,:,:) + reshape(S_avg, 1, 1, size(S_avg, 1), size(S_avg, 2)) ;

or if you know the size of S_avg for sure

Main(chan_no,level,:,:) = Main(chan_no,level,:,:) + reshape(S_avg, 1, 1, 45, 14) ;

Upvotes: 0

Related Questions