What format does matlab need for n-dimensional data input? - file

I have a 4-dimensional dictionary I made with a Python script for a data mining project I'm working on, and I want to read the data into Matlab to do some statistical tests on the data.
To read a 2-dimensional matrix is trivial. I figured that since my first dimension is only 4-deep, I could just write each slice of it out to a separate file (4 files total) with each file having many 2-dimensional slices, looking something like this:
2 3 6
4 5 8
6 7 3
1 4 3
6 6 7
8 9 0
This however does not work, and matlab reads it as a single continuous 6 x 3 matrix. I even took a look a dlmread but could not figure out how to get it do what I wanted. How do I format this so I can put 3 (or preferably more) dimensions in a single file?

A simple solution is to create a file with two lines only: the first line contains the target array size, the second line contains all your data. Then, all you need to do is reshape the data.
Say your file is
3 2 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
You do the following to read the array into the variable data
fid = fopen('myFile'); %# open the file (don't forget the extension)
arraySize = str2num(fgetl(fid)); %# read the first line, convert to numbers
data = str2num(fgetl(fid)); %# read the second line
data = reshape(data,arraySize); %# reshape the data
fclose(fid); %# close the file
Have a look at data to see how Matlab orders elements in multidimensional arrays.

Matlab stores data column wise. So from your example (assuming its a 3x2x3 matrix), matlab will store it as first, second and third column from the first "slice", followed by the first, second third columns from the second slice and so on like this
2
4
3
5
6
8
6
1
7
4
3
3
6
8
6
9
7
0
So you can write the data out like this from python (I don't know how) and then read it into matlab. Then you can reshape it back into a 3x2x3 matrix and you'll retain your correct ordering.

Related

Mean values of a matrix in a matrix on Matlab

This is about matlab.
Let's say I have a matrix like this
A = [1,2,3,4,5;6,7,8,9,10;11,12,13,14,15]‍
Now I want to know how to get a mean value of a small matrix in A.
Like a mean of the matrix located upper left side [1,2;6,7]
The only way I could think of is cut out the part I want to get a value from like this
X = A(1:2,:);
XY = X(:,1:2);
and mean the values column wise Mcol = mean(XY);.
and finally get a mean of the part by meaning Mcol row-wise.
Mrow = mean(Mcol,2);
I don't think this is a smart way to do this so it would be great if someone helps me make it smarter and faster.
Your procedure is correct. Some small improvements are:
Get XY using indexing in a single step: XY = A(1:2, 1:2)
Replace the two calls to mean by a single one on the linearized submatrix: mean(XY(:)).
Avoid creating XY. In this case you can linearize using reshape as follows: mean(reshape(A(1:2, 1:2), 1, [])).
If you want to do this for all overlapping submatrices, im2col from the Image Processing Toolbox may be handy:
submatrix_size = [2 2];
A_sub = im2col(A, submatrix_size);
gives
A_sub =
1 6 2 7 3 8 4 9
6 11 7 12 8 13 9 14
2 7 3 8 4 9 5 10
7 12 8 13 9 14 10 15
that is, each column is one of the submatrices linearized. So now you only need mean(A_sub, 1) to get the means of all submatrices.

EXCEL VBA: Removing rows from an array and adding those rows to another array

I am working in EXCEL VBA.
I have a 2 dimensional array (for this example, let's say its a 5 x 5 one-based array). This is my raw data (Array "A"):
6 7 7 8 5
9 9 9 9 7
1 3 6 9 3
7 3 2 9 9
4 9 6 5 2
I also have a separate array whose row space mirrors that of the first (e.g., a 5 x 3 one-based array). The 1st column of this array is the row number of the raw data (A). This is my meta data (Array "B"):
1 0 0
2 1 0
3 0 0
4 0 0
5 1 0
For every occurrence of "1" in the 2nd column of the meta data array (B), I need to remove the corresponding row from my raw data array (A) AND add that row to a third array (Array "C")(which will not contain any data at the beginning of this process). Therefore, in this example, I need to remove rows 2 & 5 from Array A and place them in Array C.
I also need to copy the 1st column of the Array B (the original row numbers of Array A) to both arrays A & C so that after some further processing I can re-combine the results and return the data to its original order.
I'm not sure how best to go about this. Any suggestions?
Thanks!

File format of 2d array octave

I have the following 2d array:
1 2 3
4 5 6
7 8 9
stored in a text file in format: [1 2 3; 4 5 6; 7 8 9;]. However when I try to load this file and save to a variable using:
a = load('data.txt'), it gives me following error:
error: load: unable to determine file format of 'data.txt'
Any suggestion on this would be nice. Thanks.
load only handles ASCII data if it's in the format shown in the first part of your post.
data.txt
1 2 3
4 5 6
7 8 9
And read it using:
data = load('data.txt', '-ascii');
If your data is stored as a formatted string rather than the ASCII matrix shown above, you'll have to read the file in a string and then use str2num to convert it to a 2D array.
fid = fopen('data.txt', 'r');
data = str2num(fread(fid, '*char').');
fclose(fid);
In the future, I would recommend storing matrices as ASCII as shown in the top part of the post

Easiest way to create arrays based on repeating character positions

I want to group my elements using the repeated segments in the array. The breaking is basically depend on where the repeated segments are, in my real data contains ~10000 elements and I want to know if there is a easier way to do that.
Here is a short example to clarify what I want:
Let's say I have an array,
A=[1 5 3 4 4 4 6 9 8 8 9 5 2];
What I want is to break A into [1 5 3],[6 9], and [9 5 2];
What is the easiest to code this using matlab??
Thanks.
For a vectorized solution, you can find out the places where either forward or backward differences to the neighbor are zero, and then use bwlabel (from the Image Processing Toolbox) and accumarray to gather the data.
A=[1 5 3 4 4 4 6 9 8 8 9 5 2];
d = diff(A)==0;
%# combine forward and backward difference
%# and invert to identify non-repeating elments
goodIdx = ~([d,false]|[false,d]);
%# create list of group labels using bwlabel
groupIdx = bwlabel(goodIdx);
%# distribute the data into cell arrays
%# note that the first to inputs should be n-by-1
B = accumarray(groupIdx(goodIdx)',A(goodIdx)',[],#(x){x})
EDIT
Replace the last two lines of code with the following if you want the repeating elements to appear in the cell array as well
groupIdx = cumsum([1,abs(diff(goodIdx))]);
B = accumarray(groupIdx',A',[],#(x){x})
EDIT2
If you want to be able to split consecutive groups of identical numbers as well, you need to calculate groupIdx as follows:
groupIdx = cumsum([1,abs(diff(goodIdx))|~d.*~goodIdx(2:end)])
Here is a solution that works if I understand the question correctly. It can probably be optimised further.
A=[1 5 3 4 4 4 6 9 8 8 9 5 2];
% //First get logical array of non consecutive numbers
x = [1 (diff(A)~=0)];
for nn=1:numel(A)
if ~x(nn)
if x(nn-1)
x(nn-1)=0;
end
end
end
% //Make a cell array using the logical array
y = 1+[0 cumsum(diff(find(x))~=1)];
x(x~=0) = y;
for kk = unique(y)
B{kk} = A(x==kk);
end
B{:}

Read Text File of integers into One 2D matrix in Matlab

I Matlab I want to read a File of the following format :
1 23 6 547 8 9 .....
56 56 85 2 7 9 ....
I want to read this file and store it in a 2D array that has the same shape as the file. Reading in Matlab seems complicated as it has conversions ratios! and other stuff
Any Help
Maybe you are looking for the function importdata
importdata('test.txt')
ans =
1 2
3 4
You can also use load (assuming a text file named 'a'):
load a

Resources