Is it possible to select only particular columns of a matrix? E.g. I have a 10x100 shaped matrix and I only would like to get these 4 columns: 231, 82, 12, 493.
Yes, it is possible. If your matrix is named A then A(:, [3,7,12,89]) will retrieve the columns numbered 3, 7, 12, and 89.
Related
I basically have two columns (arrays): column A represents a continuous stream of data across points in time (e.g. blood pressure rising and falling), while column B represents onset of an event (e.g. a shock or a deep breath). Column A has values for every cell, while column B only has values at a time point where an event occurred, which represent codes for the onset of different kinds of events (e.g. 1, 2, 3, 4, 5 for 5 kinds).
What code can use the values in column B to subset data in column A (say collect all data from any time points between an event 1 and 2, and event 1 and 3, or event 1 and 4)? Basically, I'm trying to pull out the values for only certain time period segments, and store them in a cell array.
Example:
What I have:
Array A: 10, 12, 13, 20, 15, 16, 14, 9, 8, 11, 12, 15, 14
Array B: 1, 0, 2, 0, 0, 0, 1, 0, 2, 0, 1, 0, 2
*(where in Array B, 1 and 2 are events--say a showing a cue and a subject
responding to that cue--and I want the data between a 1 and a 2)*
What I want:
(New) Cell Array C: [12, 13] , [9, 8] , [15,14]
*That is, it's grabbing the data from Array A, based on what falls between
1s and 2s in Array B, and storing them into cells of Array C*
Many thanks!
Here's a way:
Find the indices of starts and ends. This can be done using strfind, which also works with numeric arrays.
Use those indices to build the result with a loop.
ind_start = strfind(B, [1 0]); % Or: ind_start = strfind(char(B+48), '10');
ind_end = strfind(B, 2); % Or: ind_end = strfind(char(B+48), '2')
result = cell(size(ind_start));
for k = 1:numel(ind_start)
result{k} = A(ind_start(k)+1:ind_end(k));
end
I am using the function
in = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
lag = 8;
out = xcorr(in, lag)
it produces the output:
out = [175,000000000000, 238,000000000000, 308, 384, 465, 550, 638, 728, 819, 728, 638, 550, 465, 384, 308, 238, 175,000000000000];
I do not understand from Matlabs documentation how to get those values. Is there any kind of formula that I can use for that?
In general matlab documentation put the formulas in a chapter named More about, look at this chapter to understand which formula matlab implements.
This is the link to the More about chapter of the xcorr function.
https://it.mathworks.com/help/signal/ref/xcorr.html#bubr0h6
For greater clarity look at this code:
in = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
lag = 8;
N = length(in);
correlation = zeros(2*lag,1);
for m = -8:8
correlation(m+8+1) = sum(in.*[zeros(1,abs(m)) in(1:N-abs(m))]);
end
where sum(in.*[zeros(1,abs(m)) in(1:N-abs(m))]); computes the sum of the product beetween in and its shifted version. To compute the shifted version of in simply padding the first m elements with zero and the N-m element are in(1:N-m). I've used the abs because the lag m is either negative or positive.
Try the code and also print [zeros(1,abs(m)) in(1:N-abs(m))] for various value of m to understand better how look the shifted version of the vector.
For homework: why we use [zeros(1,abs(m)) in(1:N-abs(m))] and not [zeros(1,abs(m)) in(1:N)]?
P.s in this case you are calculating the autocorrelation, so the y vector is x.
For more details about the theory check the Reference chapter to see which books matlab refers.
I have a matrix named eta (54×1800). For selecting specific rows and columns typically we use:
result = eta(:, 86:90:1800);
But here I need to select consecutive 5 columns 86,87,88,89,90 each having difference 90. e.g after 86, 87, 88, 89, 90, I want to get 176, 177, 178, 179, 180.
I tried this:
result=eta(:,[86:90:1800,87:90:1800,88:90:1800,89:90:1800,90:90:1800]);
But it does not give the result of consecutive columns.
If your first index is a(=86), the end of region to extract is b(=1800) and the difference is d(=90), then you would do:
s = a:d:b; % create all start indices
k = cumsum([s; ones(4,numel(s))],1) % compute all consecutive indices
result = eta(:,k(:)); % exctract all indeces using linear index for the column subscript
try this
mat=rand(54,1800); %your eta matrix
mywish=[86:1:90]; %your wish to select consective columns
for i=1:length(mywish)
results=mat(:,mywish(i):90:1800) %getting the column interval 90
end
So say I have the below matrix
[1, 2, 3,
4, 5, 6,
7, 8, 9]
And I have a vector [1,3]
I want to access the 1st and 3rd row which would return
[1,2,3
7,8,9]
I need to be able to scale this up to about 1000 rows being grabbed based on values in the vector.
if A is your matrix and v your vector of index, you just have to do A(v,:)
This question already has answers here:
Reshaping of Array in MATLAB
(3 answers)
Closed 7 years ago.
Here is what I have:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
And here is what I want to get:
[
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12
]
The number of rows and columns (3 and 4 in example) is already known.
How would I do that?
reshape
b = reshape(a, 4, 3)' will would work for your example. Elements are taken from the original and inserted into the new matrix column-wise.
Furthermore, reshape is a built-in MATLAB function. There exists other solutions such as vec2mat that require the communications toolbox.
This guide says
mat = vec2mat(vec,matcol) converts the vector vec into a matrix with matcol columns, creating one row at a time. If the length of vec is not a multiple of matcol, then extra zeros are placed in the last row of mat. The matrix mat has ceil(length(vec)/matcol) rows.