I'm not sure why I can't do this in Scilab.
-->foo=zeros(500);
-->foo(300)
!--error 21
Invalid index.
Why do I get the 'Invalid index' error? I thought I had initialized foo as an array with 500 elements, each of which was set to 0?
In Scilab, you have to give both the number of rows as well as the number of columns. So, if you want to create a 500x500 matrix, you need to say zeros(500, 500). If you want a 500x1 vector, you need to say zeros(500, 1).
If you want to create a zeros matrix that has precisely as many rows and columns as another matrix (say A), you need to say zeros(A). This is where the confusion stems from.
In Scilab, zeros(500) would take 500 as a 1x1 matrix and generate a zeros matrix of size 1x1, that is [0]. In MATLAB, zeros(500) would take 500 to be the size of the matrix required, assuming a square matrix.
If zeros in Scilab behaves just like zeros in Matlab the call zeros(500) creates a 500x500 array of 0s. That said, foo(300) would be a valid Matlab expression as Matlab understands what it calls 'linear indexing' on arrays of rank greater than 1.
If zeros in Scilab does bot behave just like zeros in Matlab I can't help.
printf("%d\n",Md(y,u))
!--error 21
Índice inválido.
at line 69 of exec file called by :
como soluciono esto?
Related
It seems like this problem should be common, but I haven't found a good duplicate...
I'm implementing a level 2 S-function with a variable-sized multidimensional output. The state has to be in fixed-size Dwork vectors, so I zero-pad the input matrix to the maximum size allowed for the input and then reshape it to a vector.
When I reshape it back to a matrix for output, I need to trim it back down to the correct size.
The function needs to be general enough to support an arbitrary number of dimensions. The size of the output is stored in a size array.
For example, I may have a 500x500 matrix N, and a size array S = [40 25]. I need a MATLAB expression that would give me N(1:S(1), 1:S(2)), but it needs to work for any number of dimensions so I can't simply hardcode it like that.
Here is a solution in m-code:
%your input
M=rand(10,10,10);
S=[2,3,4]
%generate indices:
Index=arrayfun(#(x)(1:x),S,'uni',0)
%use comma separated list to index:
smallM=M(Index{:})
I'm not getting the correct results from the Matlab function so maybe my data arrangement is wrong. I looked at the help file of the function I am using and the input, "X" that it takes must be in the form.
The rows of X correspond to observations, and columns correspond to
variables.
I am sorry if this is very basic but how exactly should my input matrix be arranged?
I have 5 writers, each have a feature vector of length 18 (for example for the sake of simplicity).
So I assumed that by observations it is meant the different features of the same writer and variables mean the writers, so I arranged the input matrix as [18 x 5] where each column is a writer.
This example is simple. What of in the case of SIFT features? where each writer will produce a feature matrix [128 x num. of keypoints] which usually becomes [128 x 70] for one image. So if I want to concatenate all of them into the input matrix my input matrix will become [128 x 350].
Will this just be the input matrix X? Then in the case of SIFT each variable in 70 columns wide.
Thank you in advance
If all of your writers data have different size, I suggest you to use cell() which is cell array. http://www.mathworks.com/help/matlab/cell-arrays.html - here is your reference. So for example if you need to calculate covariance you can do it for each matrix separately. Then your covariance matrices will be same size(128*128) so you can put them together and have your 3D matrix data.
Hope it will help you.
I'm trying to multiply two complex matrices (b+c*i), but I'm getting no results.
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==> impedancaZ at 14
l=mtimes(R1,h)
I don't understand this error, as matrix dimension are the same (2 coluoms and 9 rows)
Can you help me?
Inner matrix dimensions must agree means that the inner dimensions of the matrices must match. If the first matrix has dimensions 2x9, then the second would need to be 9x(something). that's just basic liner algebra/matrix multiplication. In that case, you'll need to figure out what the second array should be. Maybe it's the transpose of what you expect it to be; instead of x*y, you might want x*y' (see the "prime" marker after the y?
Alternatively, maybe you want a "scalar multiply" rather than a "matrix multiply" for this. That is, you don't want to multiply the matrices x and y in the "linear algebra" sense, but you just want to multiply the elements of the arrays, element by element. In that case, you'd do x.*y (see the dot before the *?).
Unfortunately, I can't tell which is really correct for your situation without more context. You'll either have to supply some more information or figure it out yourself from the hints I've given.
I have to two evenly sized very large vectors (columns) A and B. I would like to divide vector A by vector B. This will give me a large matrix AxB filled with zeros, except the last column. This column contains the values I'm interested in. When I simple divide the vectors in a Matlab script, I run out of memory. Probably because the matrix AxB becomes very large. Probably I can prevent this from happening by repeating the following:
calculating the first row of matrix AxB
filter the last value and put it into another vector C.
delete the used row of matrix AxB
redo step 1-4 for all rows in vector A
How can I make a loop which does this?
You're question doesn't make it clear what you are trying to do, although it sounds like you want to do an element wise division.
Try:
C = A./B
"Matrix product AxB" and "dividing vectors" are distinct operations.
If we understood this correctly, what you do want to calculate is "C = last column from AxB", such that:
lastcolsel=zeros(size(B,2),1)
C=(A*B)*lastcolsel
If that code breaks your memory limit, recall that matrix product is associative (MxN)xP = Mx(NxP). Simplifying your example, we get:
lastcolsel=zeros(size(B,2),1)
simplifier=B*lastcolsel
C=A*simplifier
preface: As the matlab guiderules state, Usually, when one wants to efficiently populate a sparse matrix in matlab, he should create a vector of indexes into the matrix and a vector of values he wants to assign, and then concentrate all the assignments into one atomic operation, so as to allow matlab to "prepare" the matrix in advance and optimize the assignment speed. A simple example:
A=sparse([]);
inds=some_index_generating_method();
vals=some_value_generating_method();
A(inds)=vals;
My question: what can I do in the case where inds contain overlapping indexes, i.e inds=[4 17 8 17 9] where 17 repeats twice.
In this case, what I would want to happen is that the matrix would be assigned the addition of all the values that are mapped to the same index, i.e for the previous example
A(17)=vals(2)+vals(4) %as inds(2)==inds(4)
Is there any straightforward and, most importantly, fast way to achieve this? I have no way of generating the indexes and values in a "smarter" way.
This might help:
S = sparse(i,j,s,m,n,nzmax) uses vectors i, j, and s to generate an m-by-n sparse matrix such that S(i(k),j(k)) = s(k), with space allocated for nzmax nonzeros. Vectors i, j, and s are all the same length. Any elements of s that are zero are ignored, along with the corresponding values of i and j. Any elements of s that have duplicate values of i and j are added together.
See more at MATLAB documentation for sparse function