* I need the output to be a matrix because a much larger part of the code needs the output of this code to be a matrix for it's input*
Hello. I have a question related to matrix manipulation.
I need to keep the order the same in the matrix. Please see below for what I am trying to do.
lets say I start out with 3 fruits:
fruits = {'apple','orange','berry'};
and the amounts of each fruit:
amount = [3,5,2]
then the next day the amount changes:
amount = [2,4,3]
so now, my matrix will be:
3 5 2
2 4 3
but what if the next day I needed to add another fruit:
fruits = {'apple','orange','berry','banana'};
and the amounts are:
amount = [3,4,2,1]
how do I make my new matrix like this:
3 5 2 NaN
2 4 3 NaN
3 4 2 1
then on the next day, I was not given 1 of the original fruits:
fruits = {'apple','berry','banana'};
and the amount would be:
amount = [5,1,4]
then I need the matrix to be like the following:
3 5 2 NaN
2 4 3 NaN
3 4 2 1
5 NaN 1 4
How would I write the code for it to be able to handle all of these situations?
use structures then convert struct2cell and convert empties to NaNs
Related
I'm using Python 3.8.8
I have a DataFrame structured like this:
A
B
0
1
1
2
2
1
3
7
4
7
5
8
and an array:
C = [3, 4, 7]
I would like to add an array "C" as a new column to the DataFrame. The problem is this array has a different length of index than the df. I would like to make up for the difference in length in C by filling the empty cells with NaNs. My desired result would look something like:
A
B
C
0
1
NaN
1
2
NaN
2
1
3
3
7
4
4
7
7
5
8
NaN
What I am looking for specifically is a way to add C starting at a specific index of the df, but I don't know how to work around the discrepancy between the length of the df and array.
Thank you for your time
To get around the problem of 'different length' when putting your list into the dataframe, you can convert it to a pandas series. Once you do that, you can easily add it to your dataframe with the rest of the values being filled with np.nan.
In your case, you can specifically also set the index when you convert your C list to a series, which you can then assign to your dataframe. Pandas nature to align data on indices will place the series on the right index
Consider using the code below:
c = pd.Series([3, 4, 7],index=[2,3,4])
df['C'] = c
prints:
A B 0
0 0 1 NaN
1 1 2 NaN
2 2 1 3.0
3 3 7 4.0
4 4 7 7.0
5 5 8 NaN
Renaming 0 should be trivial.
I have a cell array called output. Each cell within output contains a 1024 x 1024 matrix. I would like to threshold each matrix so that elements below a given value are set to NaN.
I tried using:
output(output < 100000) = NaN;
However, I feel that this is the wrong approach. Intuitively, I want to use a for loop, however, I don't think that will be the most efficient method possible.
Thoughts? Suggestions?
Thanks :)
it can be done with cellfun function!cell fun can implement a function on every cell (it's like for loop) Assume below example
first consider you have a variable named a in cell form.
a{1,1} =
1 2
3 4
a{2,1} =
1 2
5 5
a{1,2} =
4 5
1 2
a{2,2} =
5 5
5 5
in this cell i want to substitute entries with NaN if entry lower than 3
So I write below function for this purpose
function out = main_func()
%% define a
a{1,1}=[1 2;3 4];
a{1,2}=[4 5;1 2];
a{2,1}=[1 2;5 5];
a{2,2}=[5 5;5 5];
out=cellfun(#(T) cell_f(T),a,'uniformOutput',false); % using cell fun function
function x = cell_f(x)
x(x<3)=nan; % if entries lower that 3 then substitute with Nan
the output will be like below
ans{1,1} =
NaN NaN
3 4
ans{2,1} =
NaN NaN
5 5
ans{1,2} =
4 5
NaN NaN
ans{2,2} =
5 5
5 5
I have a data set of size 11490x1. the data is recorded every 0.25 second(i.e. 4hz). So, 1 second accounts for 4 data points. The goal here is to further create sub sets every 3 seconds, meaning that I want to look at data every 3 seconds and analyze it. for example: if I had data such as [1 2 3 4 5 6 8 2 4 2 4 3 2 4 2 5 2 5 24 2 5 1 5 1], I want to have a sub set [1 2 3 4 5 6 8 2 4 2 4 3 ] and so on...
Any help would be appreciate.
It really depends on how you plan to "analyse" your data. The simplest way is to use a loop:
n = 4*3;
breaks = 0:n:numel(data)
for i = 1:numel(breaks)-1
sub = data(breaks(i)+1:breaks(i+1));
%// do analysis
%// OR sub{i} = data(breaks(i)+1:breaks(i+1));
end
A vectorized approach might use reshape(data,[],12) after padding data so that mod(numel(data),12)==0
A third way might be to break your matrix up into a cell array using mat2cell or in a for loop like above but instead of sub=... rather use sub{i}=...
Have sales and a time indicator as such:
time sales
1 6
2 7
1 5
3 4
2 4
5 7
4 3
3 2
5 1
5 4
3 1
4 9
1 8
I want the mean, stdev, and N of the above saved in a t (each time period has a row) X 4 (time period, mean, stdev, N) matrix.
For time = 5 the matrix would be:
time mean stdev N
... ... ... ...
5 4 3 3
... ... ... ...
Just for the mean I tried:
mat t1=J(5,1,0)
forval i = 1/5 {
summ sales if time == `i'
mat t1[`i']=r(mean)
}
However, I kept getting an error. Even if it worked I was unsure how to get the other (stdev and N) variables of interest.
You were probably aiming for something like
matrix t1 = J(5, 1, .)
forvalues i = 1/5 {
summarize sales if time == `i'
matrix t1[`i', 1] = r(mean)
}
matrix list t1
U[14.9] Subscripting specifies you need matname[r,c]. You were leaving out the second subscript. In Mata you are allowed to subscript vectors in this way but you never enter Mata.
An alternative is
forval i = 1/5 {
summarize sales if time == `i'
matrix t1 = (nullmat(t1) \ r(mean))
}
With the latter, you have no need of declaring the matrix beforehand. See help nullmat().
But it's probably easiest to use collapse and get all information in one step:
clear all
set more off
input ///
time sales
1 6
2 7
1 5
3 4
2 4
5 7
4 3
3 2
5 1
5 4
3 1
4 9
1 8
end
collapse (mean) msales=sales (sd) sdsales=sales ///
(count) csales=sales, by(time)
list
Note that count counts nonmissing observations only.
If you want a matrix then convert the variables using mkmat, after the collapse:
mkmat time msales sdsales csales, matrix(summatrix)
matrix list summatrix
I am new to MatLab and programming in general. I have been set the following problem:
4 players take part in a competition each person starts with 100 points.
Each person randomly plays another player.
If a player wins they get 1 point and the losing player loses 1 point from their total.
When a player has zero points they are elimated.
The game is over when there is only 1 player left.
I assume I need to create some sort of matrix array with 4 players and their 4 scores
A = [1 2 3 4; 100 100 100 100]
Is this correct and how would i remove a player and their score once it reaches zero.
Any help would be greatly recieved. Thanks
I think you are on the right track, assuming you define A as such, you can remove the players with nonpositive score as such:
A = [1 2 3 4; 100 100 0 100]
idx = A(2,:)<0.01 %To prevent suffering from rounding errors
A = A(:,~idx)
I just came across this page while looking for this answer myself (I'm also new to Matlab).
However, the above answers seem too complicated; Matlab's matrix addressing and assignment operators solve this problem very simply, as I found out after a bit of experimentation:
A = [1 2 3 4; 100 0 100 100]
A =
1 2 3 4
100 0 100 100
A(:,A(2,:)==0)=[]
A =
1 3 4
100 100 100
I hope this is useful to someone.
The 1st answer is absolutely correct. However, in order to approach this problem, there is a straight-forward syntax built for this purpose, using sparse you will also be able to extract the player with 0-points directly:
A = [1 2 3 4; 100 100 0 100]
ans= sparse (A(2,:))
Good luck ;)