I am getting error in the expression: X(j) = F*X(j-1) + w; as, In an assignment A(I) = B, the number of elements in B and I must be the same, please help?

1 visualización (últimos 30 días)
X = zeros(4,1); w = randn(4,1);
F = [ 1 0 1 0 ; 0 1 0 1 ; 0 0 1 0 ; 0 0 0 1 ];
for j=2:11;
X(j) = F*X(j-1) + w;
end;
error: In an assignment A(I) = B, the number of elements in B and I must be the same.

Respuestas (1)

Stephen23
Stephen23 el 29 de Ag. de 2016
Editada: Stephen23 el 29 de Ag. de 2016
Have a look at the sizes of the LHS and RHS of that assignment:
>> F*X_0 + w
ans =
0.20794
2.05687
0.88528
0.88327
>> X(1)
ans = 0
so the RHS has size 4x1 and the LHS has size 1x1. How do expect to put four elements into one ? An array element is one atomic unit: it cannot be split or merged. If you want to have four elements then these need to be allocated into four elements, so perhaps you meant to do this:
X(1:4) = ...
  4 comentarios
Stephen23
Stephen23 el 29 de Ag. de 2016
Editada: Stephen23 el 29 de Ag. de 2016
You write "this value...", but you don't have "this value" (i.e. one value), you actually have multiple values in a matrix of size 4x4 (based on your edited question). Am I supposed to know what you want to do with all of them?
I can't decide what you need to do because I have no idea what your algorithm is, or what you want to calculate, or what your desired output should be. You just showed us some broken code. Do you want me to use my crystal ball to know what you are trying to do?
The answer to your question "How should I store this value" is simple: either use indexing or reallocate that variable.
The fact that you seem to have too many values and don't know what to do with them is more of a problem. You need to think about your algorithm (again, perhaps you know what your algorithm is, but you certainly haven't told us). For example:
  • Inside your loop you access one element of X using X(k-1). Is this correct?
  • The result of this F*X(k-1) is size 4x4, but w has size 4x1, so you cannot add these with a simple +. What do you intend to have as the result?
  • Once you have calculated this matrix, what do you want to do with it? Which of its values do you want to use on the next iteration?
However I would suggest that you simply explain what you are trying to achieve, complete with input and output example matrices for us to test our code on, then we can show you how to achieve what you want.
Stephen23
Stephen23 el 29 de Ag. de 2016
Editada: Stephen23 el 29 de Ag. de 2016
I can't find F*X(j-1) + w anywhere in your code, even though this is what you ask about in your question. However I did find F*X(j-1) + w(j), but is a different calculation and is not what you asked about.
I would suggest that you start again. This time write your code properly:
  • Read the documentation for each function and operator.
  • Check that the outputs and inputs are exactly as you need them to be.
  • Check every line as you write it.
  • Build a set of test cases as you write the code.
  • Write code comments with clear explanations.
  • Write a help section.
Although many beginners think that having lots of code in their script means that they have done some useful work, in fact they can be more productive by actually understanding the problem, the tools, the algorithms, the requirements, etc, before they even single character to their script. Someone who understands an algorithm will be more productive than someone who simply puts lots of code on a page, but has no idea what it is doing, how it works, or why it does not work.
There is no "easy" way to do this: it requires learning, reading, thinking,...
You need to learn about ND arrays and indexing: these will help you to solve your task.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by