Matlab Linspace Problem: How to Control Ky-Points in Loops | Error Fix

  • MATLAB
  • Thread starter hokhani
  • Start date
  • Tags
    Matlab
In summary, Matlab Linspace is a function used to generate a vector of linearly equally spaced points within a specified interval. It is commonly used for creating x-coordinates for plotting graphs and controlling the number of iterations in a loop. It allows for control over the number of iterations and values in each iteration, making it useful for complex mathematical calculations or data manipulation. However, using it in a loop may result in an error if the number of iterations is greater than the number of points generated. Additionally, Matlab Linspace can only generate linearly spaced points, for non-linearly spaced points, other functions such as "logspace" or "space" should be used.
  • #1
hokhani
483
8
The following Matlab program

clc
clear all
for ef=1:100
ky=linspace(-ef,ef,ef*2);
for n=1:3
r(n,:)=2*ky;
end
end


encounters this error:

? Subscripted assignment dimension mismatch.

Error in ==> testarrays at 6
r(n,:)=2*ky;


But if I use ky=linspace(-ef,ef); there is no error! why?
In other words, what should I do to control the number of ky-points in each loop?
 
Physics news on Phys.org
  • #2
Next time, please enclose your code in the proper "CODE" tags:
Code:
clc
clear all
for ef=1:100
ky=linspace(-ef,ef,ef*2);
for n=1:3
    r(n,:)=2*ky;
end
end

I guess you are doing something with r after the inner loop, otherwise your code doesn't make sense.

The problem is that the size of r is set the first time around, and won't adjust as the length of ky increases as ef changes. You need to reassing r to an array of the proper size:
Code:
clc
clear all
for ef=1:100
   ky=linspace(-ef,ef,ef*2);
   r = zeros(3,size(ky,2));
   for n=1:3
      r(n,:)=2*ky;
   end
end
 
  • Like
Likes hokhani
  • #3
Thank you very much. But I can't understand why I have to reassign the size of r in each loop? Why this problem doesn't happen for ky?
I would be grateful also if you tell me how to write in "CODE" tags.
 
  • #4
hokhani said:
Thank you very much. But I can't understand why I have to reassign the size of r in each loop? Why this problem doesn't happen for ky?
Because for ky, as for r in my version, the entire variable gets reassigned. When you write r(n, : ), the size of r is fixed: the ":" will be replaced by the indices from 1 to the second dimension of r.

hokhani said:
I would be grateful also if you tell me how to write in "CODE" tags.

Just use
[C O D E]
some code
[/C O D E]
(without the spaces between the letters in CODE).
 

Related to Matlab Linspace Problem: How to Control Ky-Points in Loops | Error Fix

1. What is Matlab Linspace?

Matlab Linspace is a function in Matlab that generates a vector of linearly equally spaced points within a specified interval. It is commonly used to create a vector of x-coordinates for plotting graphs or for creating loops with a specific number of iterations.

2. How do I control the number of Ky-points in a loop using Matlab Linspace?

To control the number of Ky-points in a loop, you can use the syntax "linspace(start, end, n)", where n is the number of points you want to generate. This will create a vector with n equally spaced points between the start and end values.

3. What is the purpose of using Matlab Linspace in a loop?

Using Matlab Linspace in a loop allows you to have control over the number of iterations and the values in each iteration. This can be useful for creating complex mathematical calculations or for manipulating data in a specific way.

4. Why am I getting an error when using Matlab Linspace in my loop?

The most common error when using Matlab Linspace in a loop is the "Index exceeds matrix dimensions" error. This occurs when the number of iterations in the loop is greater than the number of points generated by the Linspace function. To fix this, make sure the number of iterations in your loop is equal to or less than the number of points generated.

5. Can I use Matlab Linspace to generate non-linearly spaced points?

No, Matlab Linspace can only generate linearly spaced points. If you need to generate non-linearly spaced points, you can use the "logspace" or "space" functions in Matlab.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
581
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
945
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
896
  • Engineering and Comp Sci Homework Help
Replies
1
Views
950
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Back
Top