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

  • Context: MATLAB 
  • Thread starter Thread starter hokhani
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around a Matlab programming issue related to the use of the linspace function and the assignment of values to an array within nested loops. Participants explore the error encountered when trying to assign values to an array and seek clarification on the need to reassign the size of the array in each iteration of the loop.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant describes encountering a "Subscripted assignment dimension mismatch" error when trying to assign values to the array r using the variable ky generated by linspace.
  • Another participant suggests that the error arises because the size of r is fixed after its first assignment, while ky is reassigned each time the loop iterates.
  • There is a proposal to redefine r with the appropriate size within the loop to avoid the dimension mismatch error.
  • One participant expresses confusion about why the size of r must be reassigned while ky does not encounter the same issue.
  • Clarification is provided regarding the fixed size of r when using specific indexing, which contrasts with the reassignment of ky.

Areas of Agreement / Disagreement

Participants generally agree on the need to redefine the size of r in each loop iteration to match the size of ky, but there is some confusion regarding the underlying reasons for this requirement.

Contextual Notes

There are unresolved questions about the behavior of array assignments in Matlab, particularly regarding how dimensions are managed within loops and the implications of fixed versus dynamic sizing of variables.

Who May Find This Useful

Individuals working with Matlab programming, particularly those dealing with array manipulations and nested loops, may find this discussion relevant.

hokhani
Messages
601
Reaction score
22
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
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   Reactions: hokhani
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.
 
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).
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 41 ·
2
Replies
41
Views
10K