djechlin
djechlin

Reputation: 60748

Add one to every value in a matrix in Matlab

Say I want

1 2 3 
4 5 6
7 8 9

to become

2 3 4
5 6 7
8 9 10

Say the first matrix is mat. I thought mat.+1 would work but this gives

Unexpected MATLAB operator.

Is there a good way to do this?

Upvotes: 0

Views: 1784

Answers (2)

Dan
Dan

Reputation: 45741

Just add 1, you can add a scalar to a matrix:

A = [1 2 3 
     4 5 6
     7 8 9]

B = A + 1

Upvotes: 7

djechlin
djechlin

Reputation: 60748

One solution is to use ones to create a matrix of 1s and add. Use size to pass the dimensions along:

new_mat = mat + ones(size(mat))

You can also use repmat(1,size(mat)) in place of ones, which can create matrices with different values filled in.

Upvotes: 0

Related Questions