I have a structure called "variable" with the following contents.
There are some negative values inside every field of vectors. I would like to keep the value but make it positive.
Make a new variable say v11 (a 1633X1 double), having an element wise average of the fields.
Use abs with structfun to convert values to positive. Then use struct2cell and horizontally concatenate the cell contents to apply mean and assign the result to the new field v11.
variable = structfun(#abs,variable,'un',0);
v11= struct2cell(variable);
variable.v11=mean([v11{:}],2);
Related
I am trying to make a loop that will let the user create multiple matrices in which they have declared the size of the matrix as in the number of columns and rows. I have created the first part of this loop, but my issue is creating a variable whose name will change so that the matrix that was previously created in the loop will not be overwritten. I then have to multiply all the differnt matrices together.
DO n=1:number !number is the number of matrices that need to be created
WRITE(,)'Enter number of rows the matrix has'
READ(,)r
WRITE(,)'Enter number of columns'
READ(,)
REAL, DIMENSION(r,c) :: "here I need a changing variable name so it isn't overwritten every time."
I wouldn't dynamically generate new variables. It seems more like you just want to make each new variable an element of an array. Allocate an array with size equal to the number of loop iterations. It might get tricky if the variables are all 2d arrays of different dimensions, but you could certainly wrap it in some kind of structure.
I would like to know what the meaning of
col2=b1(1:end,lead);
is in MATLAB?
This is very basic stuff. We are led to assume that b1 is a 2-dimensional array. The contents of an array are indexed using brackets, so b1(1,1) will return the top left element of array b1. The first index in your example, 1:end is selecting every element in the first dimension (matlab indexes rows first, then columns). The second index, lead in your example, selects a particular column. We assume that lead has been allocated previously somewhere in the code.
Thence, b1(1:end,lead) returns to col2 a 1-dimensional array containing a subarray of b1. This could be accomplished more elegantly using col2=b1(:,lead);.
I want to reference an index of values inside the 5th column of an array like blitzer.
E.g. I want to access, say, all values of blitzer(:,5) where blitzer(:,4) < 10. This outputs an index of values. So maybe I could set blitzer5 = blitzer(:,5), and then call blitzer5(blitzer(:,4) < 10).
But is there a quick way to do this without having to create an entirely new vector? Ideally I'd like to call blitzer(:,5)[blitzer(:,4) < 10]. If so, how?
You can give vertical index from the matrix itself:
blitzer(blitzer(:,4) < 10,5)
This will give you elements from the 5-th column, where the corresponding elements in the 4-th row are less than 10.
If you want to reference the 8th element in the 5th column of an array, you can write
theElement = blitzer(8,5)
This related question How can I apply a function to every row/column of a matrix in MATLAB? seems to indicate one way to do this is using num2cell, which I kind of want to stay away from.
Here's what I want to do. I've got an index list for a triangle mesh, the indices index the vertex list.
I want to run func(a,b,c) on the first 3 indices, then the next three indices, and so on.
So I could reshape(idxs,3,[]) so now i've got my data into triplets as column vectors. But arrayfun does not do what I want it to do.
Looking for something like a column-map operator.
First, get your func properly vectorized, if necessary, such that the arguments can be lists of equal length:
vec_func = #(a,b,c)(arrayfun(#func,a,b,c))
Then, you can directly access every third element of idxs:
vec_func( idxs(1:3:end), idxs(2:3:end), idxs(3:3:end) )
I am getting stock prices from yahoo, and want to have each stock have its own time series data structure, but also don't want to have hundreds of variables, so naturally I would want to have an array, but when I do something like array = [stock1 stock2]; it actually merges the series together. How can I make a real array?
Thanks,
CP
[x x] notation in matlab is not an array, it is a vector. It is assumed that what you're putting together belongs together. What you probably want is a cell array which is indexed with a curly brace, ie myArray{1} = stock1; myArray{2} = stock2;. Reference here.
Ah, since you have row vectors, [stock1 stock2] is a concatenation. If you want to create a 2-by-x array instead, do something like this [stock1; stock2], which will place one array above the other.
Joining vectors using [x y] has different results depending on whether your vectors are rows or columns. If rows, then joining them with [x y] makes a longer row vector, but if columns, you'll get a Nx2 matrix. You should probably convert them to column vectors using the TRANSPOSE operator thus: [x' y']. Although you should check if transpose means the same thing with Time Series objects as at does with regular vectors.