Problem storing array value in MATLAB

In summary, the weight variable in the calcw function will only store a value if all elements in the x vector are less than or equal to the value of the weight variable. If any element in the x vector is greater than the value of the weight variable, MATLAB will generate an error.
  • #1
geft
148
0
I have the following function

Code:
function [W] = calcw(k1,k2,d,x)

if x<d
    W = k1*x;
elseif x>=d
    W = k1*x+2*k2*(x-d);
end;

save('varw.mat');

with the following script
Code:
k1 = 10e4;
k2 = 1.5e4;
d = 0.1;
x = 0:0.01:0.3;
    
weight = calcw(k1,k2,d,x);

plot(weight,x);

but the problem is that the W values refuse to be stored in the weight variable in the form of an array. The code works if x is a single value, but it doesn't since x is an array. Any help please?
 
Physics news on Phys.org
  • #2
When you use a comparison on an array the way you currently have it, MATLAB will only evaluate the code under the condition if it is true for every element in the array.

Code:
[COLOR="Blue"]if [/COLOR]x<d
    W = k1*x; [COLOR="SeaGreen"]% This will only happen if *all* elements of x < d[/COLOR]
[COLOR="Blue"]elseif[/COLOR] x>=d
    W = k1*x+2*k2*(x-d); [COLOR="SeaGreen"]% This will only happen if all elements of x >= d[/COLOR]
[COLOR="Blue"]end[/COLOR]
I suspect you want to create a vector W that weights the individual elements of the input x vector. You'll need a for loop and to access the individual members of the x vector then.
 
  • #3
Do you mean using something like this?

Code:
for i = 1:length(x)
weight[i] = calcw(k1,k2,d,x[i]);
end;

I keep getting parse errors for the '=' and ')'.
 
  • #4
Your syntax is incorrect. In MATLAB, to call an element of a matrix/vector, use
Code:
[COLOR="SeaGreen"]% Vector (i.e. a 1 x n or n x 1 matrix)[/COLOR]
someElement = vector(i); [COLOR="SeaGreen"]% i is some integer 1< i < length(matrix)[/COLOR]
[COLOR="SeaGreen"]% Matrix[/COLOR]
someElement = matrix(i,j); [COLOR="SeaGreen"]% i,j are ints such that 1<i<number of rows and 1<j<number of columns[/COLOR]

I would vectorize the function instead, like this:
Code:
[COLOR="Blue"]function[/COLOR] [W] = calcw(k1,k2,d,x)
    W = zeros(1,length(x)); [COLOR="SeaGreen"]% initialize W[/COLOR]
    [COLOR="Blue"]for [/COLOR]i=1:length(x);
        [COLOR="Blue"]if[/COLOR] x(i)<d
            W(i) = k1*x(i);
        [COLOR="Blue"]elseif[/COLOR] x(i)>=d
            W(i) = k1*x(i)+2*k2*(x(i)-d);
        [COLOR="Blue"]end [/COLOR][COLOR="SeaGreen"]% end if[/COLOR]
    [COLOR="Blue"]end[/COLOR] [COLOR="SeaGreen"]% end for[/COLOR]
[COLOR="Blue"]end[/COLOR] [COLOR="SeaGreen"]% end function[/COLOR]
Then you can just call:
Code:
weight = calcw(k1,k2,d,x);
 
Last edited:
  • #5
It works! Many thanks :)
 

1. Why am I getting an error when trying to store an array value in MATLAB?

This could be due to a few reasons. One common mistake is not properly initializing the array before trying to store values. Make sure to use the "zeros" or "ones" function to create an array before storing values. Another possibility is that the array index being used is out of bounds, so check to make sure the index is within the size of the array.

2. How can I efficiently store large arrays in MATLAB?

One way to efficiently store large arrays in MATLAB is to use the "sparse" function. This creates a sparse matrix, which only stores non-zero values and their corresponding indices. This can significantly reduce the memory usage for large arrays.

3. Can I store different types of data in a single array in MATLAB?

Yes, you can store different types of data in a single array in MATLAB. This is known as a cell array, and it can hold any type of data, including numbers, strings, and even other arrays. To create a cell array, use curly brackets { } instead of square brackets [ ] when defining the array.

4. How can I access and manipulate specific elements in an array in MATLAB?

To access and manipulate specific elements in an array in MATLAB, you can use indexing. Indexing allows you to specify the position of the element you want to access or manipulate. For example, to access the second element in an array, you would use array(2). You can also use indexing to assign new values to specific elements or perform operations on them.

5. Can I store a matrix or multidimensional array in MATLAB?

Yes, you can store a matrix or multidimensional array in MATLAB. To create a matrix, use the semicolon (;) to separate rows and commas (,) to separate columns. For example, [1, 2, 3; 4, 5, 6] would create a 2x3 matrix. To create a multidimensional array, you can use the "reshape" function to convert a vector into a multidimensional array or use the "reshape" function to combine multiple matrices into a multidimensional array.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Astronomy and Astrophysics
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
987
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Atomic and Condensed Matter
Replies
3
Views
844
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
Back
Top