Table template on matlab - matlab

I need to show in the LiveScript a table using a 1x4x8 tensor and I'd like to have each row that is the third dimension of the tensor (so 8 rows) and 4 columns but if I use this
table(tensor)
I see
while if i specify the third dimension I need to create I need o create a new table for each row.
table(tensor(:,:,1)
I'm sure there is a clever way to do that. Any help?

Related

Indexing a grid with row and column number?

Very new to QGIS. I'm using it to analyse agricultural field microplots for research.
I need to give each field plot its own object which I've done so by using the "create grid" tool. But I need to assign a column and row number to each object in the attribute table to make it easier to sort the data. This should start in the bottom left (1:1) and ascend in row for objects above (2:1) and ascend in column for objects to the right (1:2).
I've orientated the grid to start in the bottom left and learned that by using #row_number, I can re-number the objects from 1 to the total number of objects. But I need to add column number and row number to the attribute table.
Is there a simple way of doing this?
I think the most accessible option here is using the field calculator to add two integer columns to the attribute table. For a rectilinear grid with cells_y cells in the y direction (24 in this case) the expressions are something like
floor(("id" - 1) / cells_y) + 1
where id is the attribute table column with the cell number (set automatically by Vector -> Research Tools -> Create grid or, in the OP's case, to #rownumber).
For the y index it's
cells_y + 1 - if(("id" % cells_y) = 0, cells_y, "id" % cells_y)
This is for the ones based indexing asked for here; remove the + 1 bits for zero-based. Similarly, subtracting (as is done with y here) or not subtracting (x here) the modulus part flips the directions in which the cells are numbered. This approach can also be used in PyQGIS.
There's a few grid plugins for QGIS and several similar StackOverflow questions (like this one) which may also be of interest.

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.)

Extracting and arranging nested data within cell array

I need to ingest a CDF (common data format) file into MATLAB. I have used the [cdfread][1] command for this purpose. An image of my output is attached below:
When I open data_import, columns 4 and 5 are in a particular 3 x 1 format (as shown in data_import(1,4)).
My question is: Is there a simple way to extract the data for each cell in column 4, such that for the 2nd row in data_import(1,4), it gets inserted as a new column (i.e. column 5) in the original data (data_import)? Similarly, 3rd row in data_import(1,4) should be inserted as a new column (column 6) in the original data (data_import). This procedure should also be repeated in the original Column 5 data which also has a similar 3 x 1 structure within each cell.
I hope I'm not being too vague in what I am describing, but I'm really not sure what I'm supposed to do regarding the commands to call for the operation. Thank you in advance.
Your desired final output has columns which are made up of these cells converted from 3 x 1 arrays to 1 x 3 cell arrays and then concatenated for each row. It's easier to do the concatenation first with the elements the "wrong way round" and then transpose the final result:
data_import = [data_import(:,1:3) num2cell([data_import{:,4}; data_import{:,5}]') data_import(:,6:end)];

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

Preserving matrix columns using Matlab brush/select data tool

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.