Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Assigning matrix to indexed variable in matlab

Status
Not open for further replies.

rameshrai

Member
Hi,

can anybody help me with this, how do i assign a matrix to an indexed variable like the following in matlab-

>> for i=1:4
if(a==1)
b(i)=[0 1];

this returns error-
In an assignment A(I) = B, the number of elements in B and I must be the same.

I know b(i) must have same size of [0 1] matrix but then how do i assign b(i) to matrix?

thanks for helping
 
You are not being clear on what you are trying to do, but the following would remove the error and create a 4x2 matrix.

>>b=zeros(4,2);
for i=1:4
if(a==1)
b(i,1:2)=[0 1];
end
end
 
hi steve,

i want to assign different matrix to different indexed variable b(i)
for eg:
b(i)=[1 0]
b(i+1)=[0 1]
b(i+2)=[1 1]

so in above code,
a=0 or 1 %depends
for i=1:4
if a==0(for eg), then assign a matrix to b(i)
b(i)=[0 1]
if a==1
b(i)=[1 0]

it is just that matrix cannot be assigned to a variable which has index i

if it is not a matrix then it works,
a=0 or 1 %depends
for i=1:4
if a==0
b(i)= 4
if a==1
b(i)= 10
 
Is there any reason why what I suggested won't work for you. You can index the row of the matrix, and each row is a 1x2 matrix.
 
hi steve, could you help one more thing-

how do i assign 2x2 matrix
b=zeros(2,8);
for i=1:4;
b(i,2:2)=[1 0;1 0];
end

error: Subscripted assignment dimension mismatch.

thanks
 
If you just want to apply the same basic method and use smaller matrices to make larger matrices, just make sure you make room for all elements. For example,

b=zeros(8,2); % note that here I swapped the 8 and 2 from what you had, this is 8 rows and 2 columns
for i=1:4;
b(2*i-1:2*i,2:2)=[1 0;1 0];
end

I have a feeling you are trying to do something more closely related to "structures" which are more complicated data types. But, usually this simple approach works. You just need to make indexing formulas to locate the beginning of each sub-matrix.
 
but simple copy/paste to check the code gives error

>> b=zeros(8,2);
for i=1:4;
b(2*i-1:2*i,2:2)=[1 0;1 0];
end

Subscripted assignment dimension mismatch.
 
OK, when I cut and pasted your version, I didn't notice you changed my 1:2 to 2:2. So, it should be ...

>> b=zeros(8,2);
for i=1:4;
b(2*i-1:2*i,1:2)=[1 0;1 0];
end

I get the feeling you dont understand the syntax and the logic behind what we are doing here. You need to understand what 1:2 means in the matrix specification. The 1:2 means we start at column 1 and end at column 2. So, b(1:4,7:9) would be a sub-matrix made from rows 1, 2, 3 and 4 and from columns 7, 8 and 9.
 
A matrix, in this context, is a two dimensional rectangular array of numbers (or objects) arranged and ordered by rows and columns.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top