Preserving matrix columns using Matlab brush/select data tool - matlab

I'm working with matrices in Matlab which have five columns and several million rows. I'm interested in picking particular groups of this data. Currently I'm doing this using plot3() and the brush/select data tool.
I plot the first three columns of the matrix as X,Y, Z and highlight the matrix region I'm interested in. I then use the brush/select tool's "Create variable" tool to export that region as a new matrix.
The problem is that when I do that, the remaining two columns of the original, bigger matrix are dropped. I understand why- they weren't plotted and hence the figure tool doesn't know about them. I need all five columns of that subregion though in order to continue the processing pipeline.
I'm adding the appropriate 4th and 5th column values to the exported matrix using a horrible nested if loop approach- if columns 1, 2 and 3 match in both the original and exported matrix, attach columns 4/5 of the original matrix to the exported one. It's bad design and agonizingly slow. I know there has to be a Matlab function/trick for this- can anyone help?
Thanks!
This might help:
1. I start with matrix 1 with columns X,Y,Z,A,B
2. Using the brush/select tool, I create a new (subregion) matrix 2 with columns X,Y,Z
3. I then loop through all members of matrix 2 against all members of matrix 1. If X,Y,Z match for a pair of rows, I append A and B
from that row in matrix 1 to the appropriate row in matrix 2.
4. I become very sad as this takes forever and shows my ignorance of Matlab.

If I understand your situation correctly here is a simple way to do it:
Assuming you have a matrix like so: M = [A B C D E] where each letter is a Nx1 vector.
You select a range, this part is not really clear to me, but suppose you can create the following:
idxA,idxB and idxC, that are 1 if they are in the region and 0 otherwise.
Then you can simply use:
M(idxA&idxB&idxC,:)
and you will get the additional two columns as well.

Related

Deletion of all but the first channel in a cell of matrices

I have a row cell vector M, containing matrices in each cell. Every matrix m (matrix inside the big matrix M) is made of 2 channels (columns), of which I only want to use the first.
The approach I thought about was going through each m, check if it has 2 channels, and if that is the case delete the second channel.
Is there a way to just slice it in matlab? or loop it and obtain the matrix M as the matrix m would disappear.
First code is:
load('ECGdata.mat')
I have the below.
when I double-click in one of the variable , here is what I can see:
As you can see the length of each matrix in each cell is different. Now let's see one cell:
The loop I'm trying to get must check the shape of the matrix (I'm talking python here/ I mean if the matrix has 2 columns then delete the second) because some of the variables of the dataframe have matrix containing one column (or just a normal column).
In here I'm only showing the SR variable that has 2 columns for each matrix. Its not the case for the rest of the variables
You do not need to delete the extra "channel", what you can do is quite simple:
newVar = cellfun(#(x)x(:,1), varName, 'UniformOutput', false);
where varName is SR, VF etc. (just run this command once for each of the variables you load).
What the code above does is go over each element of the input cell (an Nx2 matrix in your example), and select the first column only. Then it stores all outputs in a new cell array. In case of matrices with a single column, there is no effect - we just get the input back.
(I apologize in advance if there is some typo / error in the code, as I am writing this answer from my phone and cannot test it. Please leave a comment if something is wrong, and I'll do my best to fix it tomorrow.)

Matlab - Generating multiple matrices by looping

I have a vector with +16M data, and I've got to transform it in 101 matrices of 401x401 elements each. I know how to create such matrices independently (writing a loop for each one of them) but I think there must be some way to create all of them in using two or more loops. The problem is, I don't know exactly how to do this.
This is what I've tried so far:
data=load('file.dat');%This file contains 3 columns of data, I only need the first one
var=data(:,1);
p=401;%Size of the matrices
for n=0:400
mat1(n+1,:)=var(p*n+1:p*(n+1),:);
end
This code would create the first 401x401 matrix. By changing the indices, I could (individually) create the rest, but I would prefer to add another loop (or loops) to create them automatically instead of repeating this code a hundred times.
The function 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.
See also the manual entry.

Export certain columns from a Matlab matrix

I have a Matlab Matrix and would like to know if there is a way to extract certain columns from this to make a new matrix.
For example, if i have a matrix of;
data=1:20
I would like to export data from columns 1,2, 9,10 and make a new matrix file.
I would like to scale this up to a matrix of about 4,400 columns...so if there is a way to select columns at defined points (like every 8th and 9th column), then that would be super!
Any help would be greatly appreciated!
Thanks,
Aj
The example you've given can be done like this:
x=data([1,2,9,10]);
You can get every 8th column like this:
index=8;
x=data(index:index:end);
If you want every 8th and 9th column and to maintain the order:
index1=8;
index2=9;
x=data(sort([index1:index1:end index2:index2:end]));
if you also want to grab individual columns or rows similar process can be used and then concatenate the into a matrix
x=data(:,2) % get the 2nd column
y=data(:,8) % get the 8th
z=[x;y] or z=[x:y] % combine them
If you table is standard you can predefine them in a small script

How to change elements in matrices using MATLAB

Starting wish a 7x4 binary matrix I need to change a random bit in each column to simulate error. Have been trying to no avail.
A very straightforward way to do this is to use a for loop. It might not be the most efficient approach in MATLAB, but it's probably good enough considering your data set is so small.
Iterate through each of the four columns. On each iteration, randomly chose a number from 1 to 7 to represent the row in that column that you have selected to change. Finally, flip the bit at that row/column. The following code does just this. Assume that "A" is a binary matrix with 7 rows and 4 columns
for col=1:4; %// Iterate through each column
row = ceil(7*rand()); %// Randomly chose a number from 1 to 7 to represent row
A(row,col) = ~A(row,col); %// Flip the bit at the specified row/col
end
Another possibility is to create 4 random numbers in one call, and assign in a vectorized fashion:
rowNumbers = randi(4,[1 4])
A(rowNumbers,:) = ~A(rowNumbers,:);

how this code is used in matlab for Artificial Intelligence

i am trying this code since last night but i cant understand what this code is doing. actually i am beginner in matlab programming
load('79.mat')
trainingData=d79;
colormap(gray);
colormap(grey);
x=reshape(d79(1234,:),28,28);
y = x(:,28:-1:1);
pcolor(y');
Kindly help me in understanding this code. :/
This is pretty simple. Here is a line-by-line explanation:
Loads data from a data file
Puts the loaded data into a variable named trainingData
Sets the colormap for plotting
Take the 1234th row of the loaded matrix, convert it into a 28-by-28 matrix, and store it in the variable x. So for example, on a smaller scale, [5 6 7 8] is converted to the matrix [5 7; 6 8] if you want to reshape that matrix to 2-by-2.
Reverse the column order of x and put that in y. So the last column becomes the first, second-last becomes second, etc.
This is a checkerboard plot of the values contained in y.
Edited to include more detail on lines 5 and 6:
The reshape line assumes that there is a row with number 1234, and that there are 784 (28*28) elements in that row. It takes that row, and makes a 28x28 matrix out of it, by taking the first 28 elements, making them into the first column of the new matrix, then taking the next 28 elements, making them the second column, and so on 26 more times. The final matrix is names x.
For the y line, like I said, it just reverses the columns of x, it puts the last column first, the second-last column second, and so on until the first column of the x is put as the last column of y.