How to make a new matrix based on column values in an old matrix? [closed] - matlab

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am very new to Matlab and coding in general, so I apologize if this is a basic question.
I have a matrix of three columns (data1) where the first column refers to time (s).
I would like to make a new matrix (bout1) consisting of entire rows of the matrix data1 based on the values in the first column (so, for example, in a range from 30 s to 120 s).
I know how to extract the rows based on the row number:
bout1 = data1(361126:391643,:)
but not based on the values in a specific column.

You can use the find function (see here) to find the rows you need, like this:
time = data1(:, 1);
i = find(30 <= time & time <= 120);
bout1 = data1(i, :);

Related

Linear Interpolation in MATLAB [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have two arrays the first one represents the time axis with time stamp 1 min
time=[0,60,60,120,180,240,300,360,420,480,540]
and the second array represents a data values as follows
data=[18,12,12,0,7,9,6,8,12,18,0]
what im trying to do is two things:
1-I would like to fix the time axis to have 1 second time stamp
2-Perform linear Interpolation as follows:
for example i have
enter image description here
and i would like to have sth like this:
enter image description here
In case of time repetation like the repated 60 seconds the duplication should be deleted
You can remove duplicates (the first value is kept) with
time = [0,60,60,120,180,240,300,360,420,480,540];
data = [18,12,12,0,7,9,6,8,12,18,0];
[time_u unique_indeces] = unique(time);
data_u = data(unique_indeces);
clear unique_indeces;
and interpolate with
time_i = linspace(min(time), max(time), max(time) - min(time) + 1);
data_i = interp1(time_u, data_u, time_i);
I prefer linspace because I usually want to set the number of data points and not the space between points but you can also use min(time):max(time) or time(1):time(end) instead.
This code will also sort your data points by time.
Function interp1 does the job:
time=[0,60,120,180,240,300,360,420,480,540];
data=[18,15,0,7,9,6,8,12,18,0];
time_1s = 0:540;
data_interpd = interp1(time, data, time_1s);
Note: I have manually deleted the first duplicate value at time 60. If there is only one value to remove (always at same place), I think the best is to remove it by using a mask, since unique removes the second occurrence of duplicates and not first one.

MATLAB: How to sort .txt data file with 1000 data points (10 collected each day for 100 days)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I loaded in the 1000x1 .txt file but I need to make it 100x10? How do I do this in matlab?
Thanks
Reshape should do this for you. https://www.mathworks.com/help/matlab/ref/reshape.html
But you haven't explained any criteria regarding what the change of dimensions is based on. So, it may not be exactly what you want.
You can use the reshape function to do this. In the example below I created a column vector x that is 1000 x 1 containing numbers that ramp from 1 to 1000. I didn't know what order you wanted the rows and columns populated, so I created variables x2 and x3 with the two variations, you can choose the form that fits your needs.
x = (1:1000)';
% x2 is created with one column at a time
x2 = reshape(x, 100, 10);
% x3 is created one row at a time
x3 = reshape(x, 10, 100)';

Is there a faster way to calculate the expected value? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
So for my probability class, my professor asked the following question on a homework problem:
A fair coin is flipped 10,000 times. Let X correspond to the difference between the number of heads and the number of tails. Using MATLAB, compute the expected value of X.
This is what I wrote to answer the question:
N = 10000;
i =0;
r=1/2;
Q=nchoosek(N,(X+N)/2);
X=(1,N);
for i=-N:N
P=Q*r.^(X+N)/2*(1-r)^(N-(X+N)/2) % probability_mass_function
E=sum(abs(X).*P); % expected value
end
However, is there faster and quicker way to compute this expected value? Any help would be greatly appreciated. Thanks
You can put all tests results in single matrix in one line and then calculate X per test, then average over X:
clear
TAIL=0; HEAD=1;
NumTests=121;
NumRollsPerTest=10*1000;
AllTestsRolls= rand(NumTests,NumRollsPerTest)>0.5 ; %head when rand>0.5
XperTest=sum(AllTestsRolls==HEAD,2)-sum(AllTestsRolls==TAIL,2);%every row is test so calc per test
ExpectedX=sum(XperTest)/length(XperTest)

Using repmat() in MATLAB [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to generate the trial order of my stimuli for an experiment consisting of 10 experimental blocks:
There should be 100 trials per block.
There are 20 images as stimuli.
Within each block, the 20 stimuli should
be shown 5 times each.
The order of the stimuli should be fully randomized.
(Meaning, NOT 1:20 in random order, then 1:20 in random order, and so on.
All 100 trials should be randomized across each block!)
I have to make a matrix that represents the trial order of my experiment, in which the rows represent the 10 blocks, and the columns represent the stimuli to
be shown in order from column 1 - to column 100.
I figured out that I should use the function repmat(), but I can't solve this.
This will do it, just adjust your values for the number of blocks and block size according to your needs. No repmat used though.
Nblocks = 10;
Nchoices = 20;
Ndisp = 5;
Ntrials = Ndisp*Nchoices;
array = ceil([Nchoices/Ntrials:Nchoices/Ntrials:Nchoices]);
perms = array(cell2mat(cellfun('randperm',mat2cell(Ntrials*ones(Nblocks,1),ones(Nblocks,1),1),'UniformOutput',0)));
It's a good idea to split the longer wrapped command into individual steps if you want to make sense of it in more depth. Look in particular at the documentation for individual functions and particularly ceil and randperm.

matlab coding for mean value of one row [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I need matlab coding for measuring mean value of one row in a matrix of 180 by 50 rows and columns. Each time the number of row in a matrix needs to be updated to get mean value of next row (like new_row=1:1:180). Kindly respond as soon as possible
You are looking for mean().
mean(A,2)
This will give you the means of each row of matrix A.
P.S.: If you already save one row in a vector, say new_row, you can simply use:
mean(new_row)