Problem assigning columns indirectly, ??? Subscripted assignment dimension mismatch

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"
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

Open in new window

ehensensAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
yuk99Connect With a Mentor Commented:
One of the solutions is below. There was one problem in the task. If we assume that number of rows in matrix b depends on N (3N), then number of rows (3N-2) is true only for N=3. The general case will be 3N-N+1. I used a(:) in repmat just to make sure a is column vector.

You can also modify the code in my previous comment, it would be another solution.
N = 3;
a = 1:N;
r=3*N;
c=3*N-N+1;
b = zeros(r,c);
idx=tril(ones(r,c))-tril(ones(r,c),-N);
b(idx>0)=repmat(a(:),c,1);

Open in new window

0
 
yuk99Commented:
First, even if you assign this in a for-loop, you can do it only until m=5. For m>5, you get vector with more elements then number of rows in b. This is probably why you have this error.
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?
0
 
LukeyJayCommented:
The reason you're getting a subscript dimension mismatch is that you're trying to assign a 2-dimensional vector to the second dimension of another 2-dimensional vector.  Instead, try doing the following:

b(:,m(:)) = [zeros(m(:)-1,1);x2;zeros(2*N-1-m(:),1)]

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
0
The new generation of project management tools

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.

 
yuk99Commented:
LukeyJay,
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.
0
 
yuk99Commented:
For some reason the code didn't attach. Here is it.
a=repmat([x2;zeros(5,1)],1,7);
b=reshape(a(1:49),7,7)

Open in new window

0
 
LukeyJayCommented:
yuk99,

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
0
 
ehensensAuthor Commented:
Thanks all for the comments; I understand that I need to be more detailed in what I want. Here it is:

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)

Open in new window

0
 
ehensensAuthor Commented:
sorry I meant size (3N)x(3N-2)
0
 
ehensensAuthor Commented:
Thank you very much.
0
 
ehensensAuthor Commented:
Wait,

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?
0
 
yuk99Commented:
Input is variable a. I assume this is vector with the length N. Correct?
 I used a=1:N only as an example.
0
 
ehensensAuthor Commented:
Very well, thank you once more and nice job.
0
All Courses

From novice to tech pro — start learning today.