I have the following array and I would like to convert it to a dataframe with the column names(Time and DepressionCount). I am pretty much a complete beginner to R and some help is greatly appreciated.
2004 2004.25 2004.5 2004.75 2005 2005.25 2005.5 2005.75
875 820 785 857 844 841 726 766
This is what you want:
dd=data.frame(t(array))
colnames(dd) = c("Time", "DepressionCount")
t(array) changes the two lines array into a two columns array, data.frame simply converts the array to a data.frame and colnames changes the column names of the data.frame.
Related
Consider that I have a code that returns a double array. When run, the result returned at the command window a result looks like this:
mpc.gen=
1 100 344 34 5
2 433 223 45 2
3 333 432 12 3
4 213 233 12 3
What I want to do is create a MATLAB script which would contain this array. In other words in this new MATLAB script I would have the following array, constructed like this:
mpc.gen= [ 1 100 344 34 5 ;
2 433 223 45 2 ;
3 333 432 12 3 ;
4 213 233 12 3 ;
] ;
Just calling the function would save the variable in the new script, however that is not what I need since I need to use this script for a simulation at a special power tool called MATPOWER. The array needs to be in the form shown at the new script.
The actual array is much bigger in size however I use this here for simplicity. Is there any way I can do this automatically, and not just pass the data from the command window to the new script one by one manually? Any help would be greatly appreciated. Thank you in advance for your time!
What you want to use is mat2str. It converts a matrix to a string representation that, when evaluated, results in the identical matrix.
For example:
>> result = [1 100 344 34 5;
2 433 223 45 2;
3 333 432 12 3;
4 213 233 12 3];
>> mat2str(result)
ans =
[1 100 344 34 5;2 433 223 45 2;3 333 432 12 3;4 213 233 12 3]
You could write this to a file like so:
f = fopen('test_script.m', 'w');
fprintf(f,'mpc.gen = %s\n', mat2str(result));
fclose(f);
The formatting is not identical to your example, but if the tool does proper MATLAB parsing, it shouldn't be a problem.
Is it intentional that mpc is a struct? (you are using dot in the name.) In the following, I use underscore instead, but it can certainly be adopted, if it is intentional.
This is what functions are for. So instead of making a script, you should create a new function. If I were to do what you want to do, I would have 2 functions and 1 script. Whereas the script looks like this
mpc_bus = function1;
function2(mpc_bus);
where function1 is the code that returns mpc.bus and function2 is the one where you want to work with mpc.gen, and the top would be something like this
function [] = function2(mpc_gen)
Alternatively: If you of some reason does not like functions, you can make the first code save the variable using save(*filename*.mat) and then you can load the file again in the script using dat=load(*filename*.mat); and mpc_gen = dat.mpc_bus;
I have some sample code where the array is sliced as follows:
A = X(:,2:300)
What does this mean about the slice of the array?
: stands for 'all' if used by itself and 2:300 gives an array of integers from 2 to 300 with a spacing of 1 (1 is implicit) in MATLAB. 2:300 is the same as 2:1:300 and you can even use any spacing you wish, for example 2:37:300 (result: [2 39 76 113 150 187 224 261 298]) to generate equally spaced numbers.
Your statement says - select every row of the matrix A and columns 2 to 300. Suggested reading
I have two double arrays like the ones below:
K>> var_conx
var_conx =
1
3
127
129
216
217
252
253
302
303
342
343
and
K>> var_cony
var_cony =
2
126
128
216
217
252
253
302
303
342
343
My task is pretty simple, I only need to store in an another double array all the common values of the two arrays, let's call the other array "common_convar".
To be specific, for the two arrays above, i only want to store the values 216,217,252,253,302,303,342,343. For the other values, i do not care and i do not want them stored or whatever.
I have written the following code:
for i=1:length(var_conx)
for j=1:length(var_cony)
if var_conx(i)==var_cony(j)
common_convar(i,:)=[var_conx(i)];
end
end
end
The problem i encountered here is that the array common_convar also stores some zeros in the beginning:
K>> common_convar
common_convar =
0
0
0
0
216
217
252
253
302
303
342
343
How is it possible to get rid of zeros and only store the desired common values of the two arrays var_conx and var_cony?
Thanks in advance for your time.
Firstly you could find the elements common to both arrays, without having to do nested loops, using the Matlab set intersect function:
common_values= intersect(var_conx,var_cony);
Then you could find the nonzero elements of the common array via logical indexing:
common_values = common_values(common_values > 0);
I gave this a search but could only find answers for when the row number is known.
I have a N by 4 matrix and am trying to remove rows based on the specific values in the second column and then the first column.
Initially I approached this the wrong way by filtering the matrix based on the values I didn't want, and then taking it away from the initial matrix, which obviously was not the result I wanted. (see below)
days = [669 680 298 299];
ind = ismember(B(:,2),days);
D = B(ind,:);
C=[B;-D];
I'm assuming there is a very similar way of removing rows, instead of filtering for them?
If you could help me in any way it would be much appreciated!
Edit:
Input:
1002 101 04 92
1002 12 12 298
1002 298 12 589
1002 680 12 589
Output:
1002 101 04 92
1002 12 12 298
I can't use row numbers as the matrix has millions of rows
To remove rows based on second column:
B(ismember(B(:,2),days),:) = [];
or equivalently
B = B(~ismember(B(:,2),days),:);
Or, in your code, change third and fourth lines into
C = B(~ind,:);
I am still a newbe and I have probabily a very easy question concerning arrays of matrices. I have a matrix of nrows like the following one:
>> matrix
1 678 543
2 676 541
3 543 987
4 543 98
1 433 54
2 908 32
3 457 54
4 235 21
How to create arrays of equal size matrices?
i.e array{i,1}
This is replication of question:
Array of Matrices in MATLAB
and probably of many others.
What is not clear to me, is how to populate my array of fixed dimension matrices. So that
>>array{1,1}
1 678 543
2 676 541
3 543 987
4 543 98
Here is my attempt:
Find all the ones in column 1 of matrix and the size of matrix.
Create cell arrays, look in each line, if it is equal to 1 create an array{i,1} of zeros equal to the size of the matrices I want to create (in my case 4x3).
If not equal to 1 insert into the array the first four values of matrix.
Is there any faster way to do it without a loop?
You can also use mat2cell:
mat2cell(matrix, [4 4])