You are Here: Home

# struggling with matlab arrays Watch

1. What's wrong with this code? I get the error
"In an assignment A(: ) = B, the number of elements in A and B must be the same." when I start assigning N1..5 to N(1..5).
Code:
```N1 = zeros([1 (5000000/div)]);
N1(1) = N0;
N2 = zeros([1 (5000000/div)]);
N3 = zeros([1 (5000000/div)]);
N4 = zeros([1 (5000000/div)]);
N5 = zeros([1 (5000000/div)]);

N = zeros([5 (5000000/div)]);

N(1) = N1;
N(2) = N2;
N(3) = N3;
N(4) = N4;
N(5) = N5;```
This is what I'm visualising; what's wrong with it?

There seems to be a similar problem with this code too; apparently the dimensions of A and v don't match. The error comes from the last line.
Code:
```v = zeros(N);
A = zeros(N,N);
for i = 1:N-1
for j = 1:N-1
v(i) = v(i) + y(j) * x(j) ^ i;
A(i, j) = x(j)^(i+j);
end
end

c = A \ v;```
2. If you want to call parts of a matrix you need to give both subscripts.

eg.
Code:
`N(i,j)`
returns the i th, j th element of N.

To return consecutive elements you can use ':'.

eg.
Code:
`N(i,1:3)`
returns elements 1 through 3 of the i th row.

To get the 1st row of N you need:
Code:
`N(1,1:end)`
or equivalently
Code:
`N(1,:)`
therefore
Code:
`N(1,:) = N1;`
will give you what you want.

I've edited this on further looking at your problem:

So you've defined v as a square matrix 'v=zeros(N)', but I think you actually want it as a vector. For this we need:
Code:
`v = zeros(1,N);`
for the same reasons as earlier.

This now gives the problem you're having in the OP. In this case you've got a row vector, but you need a column vector to solve the equation. Further, your matrix has been created as a 5 by 5 matrix, but you're only filled the first 4x4. This is resulting in a singular result.

With the information given, it's hard to tell what you actually want to produce, but you could get a solution for a 4x4 matrix. Alternatively, you could change the For conditions to i,j=1:N.

Anyway, one possibility is this (alternatively you could define v as zeros(N,1) originally instead of transposing, but I think this makes it a bit clearer):
Code:
```N = 5;
v = zeros(1,N);
A = zeros(N,N);
c = zeros(1,N)
x = [1:N];
y = [1:N];

for i = 1:N
for j = 1:N
v(i) = v(i) + y(j) * x(j) ^ i;
A(i, j) = x(j)^(i+j);
end
end

v1 = transpose(v);

c = A \ v1```
3. I've edited the above response a lot, but it's seems hard to tell what you're actually trying to do in your second question.
4. (Original post by throwawaycon)
I've edited the above response a lot, but it's seems hard to tell what you're actually trying to do in your second question.
Okay so I've come across another error in the first program.

"Error using plotVectors must be the same length.
Error in C067 (line 96) plot(t1, [N1, N2, N3, N4, N5]); "

The thing is, I checked the size of each parameter and they're all the same..

Whenever I try to run the second program, matlab crashes it's meant to curve fit polynomials. This is the full program (ignore the weird formatting):
I don't know if it does what it's supposed to..
Code:
```function [] = C002_2()
[x, y] = datagen(0,0.1,100,[1.5, -2.5, 0.7, -1.2], 0.01);
N = 100/0.1;
f = fit(x, y, N);
plot(f(1), f(2));
end
function [x, y] = datagen(start,div,fin,coeffs, e)
x = startfin;
N = (fin-start)/div;
y = zeros(N);
r1 = rand();
r2 = rand();
g = sqrt(-2*log(r1))*cos(2*pi*r2);
for i = 1:N
for j = 1:size(coeffs)
y(i) = coeffs(j) * x(i)^(j-1) + e*g;
end
end
end
function [f] = fit(x, y, N)
v = zeros(N);
A = zeros(N,N);
for i = 1:N
for j = 1:N
v(i) = v(i) + y(j) * x(j);
A(i, j) = x(j);
end
end
c = A \ v;
y = zeros(size(x));
for i = 1:size(x)
for j = 1:size(c)
y(i) = c(j) * x(i)^(j-1);
end
end
f = [x, y];
end```

TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

This forum is supported by:
Updated: April 24, 2016
Today on TSR

### What is the latest you've left an assignment

And actually passed?

### Simply having a wonderful Christmas time...

Discussions on TSR

• Latest
• ## See more of what you like on The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

• Poll
Useful resources

Can you help? Study Help unanswered threadsStudy Help rules and posting guidelines

## Groups associated with this forum:

View associated groups
Discussions on TSR

• Latest
• ## See more of what you like on The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

• The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.