With monday.comâ€™s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Hi all,

I'm having a major problem that doesn't make any sense to me. Basically, I have an array x2 = [4;5;6], a scalar N = 3, an array of zeros size 7 * 7 called b, and an array m = [1 2 3 4 5 6 7]. I want to iterate through m and assign the columns of b like my code shows. What I am doing wrong, and how can I achieve this?

Keep in mind that when I tell matlab to assign columns individually by using a number 1 through 7 in place of "m" the column is assigned correctly. It is just when I try to assign all of them with one command that it tells me, "??? Subscripted assignment dimension mismatch"

I'm having a major problem that doesn't make any sense to me. Basically, I have an array x2 = [4;5;6], a scalar N = 3, an array of zeros size 7 * 7 called b, and an array m = [1 2 3 4 5 6 7]. I want to iterate through m and assign the columns of b like my code shows. What I am doing wrong, and how can I achieve this?

Keep in mind that when I tell matlab to assign columns individually by using a number 1 through 7 in place of "m" the column is assigned correctly. It is just when I try to assign all of them with one command that it tells me, "??? Subscripted assignment dimension mismatch"

```
N = 3;
x2 = [4;5;6];
b = zeros(3*N-2,3*N-2);
m = 1:(3*N-2);
b(:,m) = [zeros(m-1,1);x2;zeros(2*N-1-m,1)]
??? Subscripted assignment dimension mismatch
```

Second, you cannot use vector as an argument for zeros function. See its documentation. b(:,m) is just the same as b, since m lists indices for all the columns.

If you want the code, please explain how it should behave when m>5. What matrix you want as a result?

b(:,m(:)) = [zeros(m(:)-1,1);x2;zeros(

Not sure if it will work, but give it a try. As for b having only 5 rows, I don't see it. I think that's ok.

Luke

As I said you cannot use vector/matrix as an argument for zeros function, only scalars are accepted, or only one argument as a size vector. If you use vector as one of the arguments, zeros will use only the first element (and give you a warning).

I didn't said b has 5 rows, it has 7 rows. But at m=6 or 7, vector on the right side of assignment will have length 8 or 9. This is where the error comes from.

To vectorize this command, it should be rewritten completely.

May be this is what ehensens wants? But I need more clarification.

```
a=repmat([x2;zeros(5,1)],1,7);
b=reshape(a(1:49),7,7)
```

You're right, b would be the wrong size. The equation for the last several rows leads to indeces 0 and -1 and -2 for m = 5 and m = 6 and m = 7, respectively, which would be invalid indeces anyway. So, as you point out, there's no way of knowing what to do unless further clarification is given. Probably, I would guess, he/she wants the values of x2 that wouldn't "fit" in the declaration to be truncated off.

By the way, my code didn't work, but in all fairness to myself, I didn't have matlab in front of me at the time. It was a shot in the dark, and it missed.

Luke

If I have a 1 dimensional matrix of any size (see code snippet), I want to make a matrix that looks like the second matrix in my code snippet, but the key is WITHOUT any loops.

I hope you can all understand how that was my intention from my original post, but I just don't know how to do it.

Thanks for any help!

```
a = [1;2;3]
b = 1 0 0 0 0 0 0
2 1 0 0 0 0 0
3 2 1 0 0 0 0
0 3 2 1 0 0 0
0 0 3 2 1 0 0
0 0 0 3 2 1 0
0 0 0 0 3 2 1
0 0 0 0 0 3 2
0 0 0 0 0 0 3
Where a is a matrix of size N and b is a matrix of size (3N-2)x(3N)
```

Upon further inspection I'm not positive this is the correct solution. The input matrix is not always [1;2;3], it can be any size and can contain any numbers. Where in your solution does it take the input matrix?

All Courses

From novice to tech pro — start learning today.

You can also modify the code in my previous comment, it would be another solution.

Open in new window