I am trying to write three different arrays into 3 columns in a csv file. This what I have tried :
str = 'This is the matrix: ' ;
a= [1 2 3 4 5 6 ]';
csvwrite('C:\Users\ganesh\Desktop\data.csv', a);
b= [11 12 13 14 15 16]';
csvwrite('C:\Users\ganesh\Desktop\data.csv', b, 1, 0);
c= [21 22 23 24 25 26]';
csvwrite('C:\Users\ganesh\Desktop\data.csv', c, 2, 0);
But it does not work. Only last data is coming that too in a row. I have tried to put offset for columns.
Did I made a mistake somewhere ? ALso is there a way I can write these data in a single function call , rather than calling three time as shown here.
You should put your data in a matrix:
m = [1 2 3 4 5 6
11 12 13 14 15 16
21 22 23 24 25 26];
csvwrite('data.csv', m);
Related
I have a matrix A in Matlab of dimension Nx(N-1), e.g.
N=5;
A=[1 2 3 4;
5 6 7 8;
9 10 11 12;
13 14 15 16;
17 18 19 20];
I want to rearrange the elements of A in a certain way. Specifically I want to create a matrix B of dimension (N-1)xN such that:
for i=1,...,N,
B(:,i) collects
1) the first i-1 elements of the i-1th column of A and
2) the last N-i elements of the ith column of A.
Notice that for i=1 the i-1th column of A does not exist and therefore 1) is skipped; similarly, for i=N theith column of A does not exist and therefore 2) is skipped.
In the example above
B=[5 1 2 3 4
9 10 6 7 8
13 14 15 11 12
17 18 19 20 16];
This code does what I want. I am asking your help to vectorise it in an efficient way.
B=zeros(N-1,N);
for i=1:N
if i>1 && i<N
step1=A(1:i-1,i-1);
step2=A(i+1:N,i);
B(:,i)=[step1;step2];
elseif i==1
B(:,i)=A(i+1:N,i);
elseif i==N
B(:,i)=A(1:i-1,i-1);
end
end
Extract the lower and upper triangular matrices of A. Then reassemble them with a "diagonal shift":
u = triu(A);
l = tril(A,-1);
B = padarray(u(1:end-1,:),[0 1],'pre') + padarray(l(2:end,:),[0 1],'post');
Another valid approach using logical indexing combined with tril and triu:
B = zeros(size(A'));
B(tril(true(size(B)))) = A(tril(true(size(A)), -1));
B(triu(true(size(B)), 1)) = A(triu(true(size(A))));
Result:
>> B
B =
5 1 2 3 4
9 10 6 7 8
13 14 15 11 12
17 18 19 20 16
Assuming we have 3 matrices, A, B and C, all are of the same size 256x256. It is known that last 20% of columns of Matrix A is identical to first 20% of Matrix B and last 10% of Matrix B is identical to first 10% of Matrix C. So in these cases since we know the overlapping amount, I do not need to compare the 3 matrices, but i want to join them at the overlap.
Taking a smaller Matrix as an example here are the 3 Matrices
A = [1 2 3 4 ; 5 6 7 8; 9 10 11 12];
B = [3 4 13 14; 7 8 15 16; 11 12 17 18];
C = [14 19 20 21; 16 22 23 24; 18 25 26 27];
So I would like my output to be
D = [1 2 3 4 13 14 19 20 21
5 6 7 8 15 16 22 23 24
9 10 11 12 17 18 25 26 27
I hope this might explain it better. I am extremely new to matlab. I tried using matrix shift, but we have only circular shift available.
Concatenation does not work because it just joins the 3 matrices. What would be the best way to overlay these 3 matrices together ?
Make proper use of matrix indexing and concatenation
For your example
D = [A B(:,3) C];
For a 256x256 Matrix and your concatenation conditions:
D = [A B(:, 0.2*256+1 : 0.9*256) C]
Since 256/10 is no integer you may adjust the index values
I have a matrix
A= [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16; 17 18 19 20]
I want to do some calculation on this matrix. But actually I do not need all the rows. So I have to discard some of the rows from the above matrix before doing a calculation. After discarding 3 rows, we will have a new matrix.
B= [1 2 3 4; 9 10 11 12; 17 18 19 20];
Now I have to use B to make some other calculations. So how can I discard some of the unwanted rows from a matrix in matlab? Any suggestion will be helpful. Thanks.
Try this: (Use when no. of rows to keep is lesser)
%// Input A
A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16; 17 18 19 20];
%// Rows (1-3,5) you wanted to keep
B = A([1:3, 5],:)
Output:
B =
1 2 3 4
5 6 7 8
9 10 11 12
17 18 19 20
Alternative: (Use when no. of rows to discard is lesser)
%// rows 2 and 3 discarded
A([2,3],:) = [];
Output:
>> A
A =
1 2 3 4
13 14 15 16
17 18 19 20
Note: Here (in the alternate method), the output replaces the original A. So you need to back up A if you need it afterwards. You could do this before discarding operation to backup Input matrix
%// Input A is backed up in B
B = A;
You can select the indices of the rows you want to keep:
A([1,3,5],:)
ans =
1 2 3 4
9 10 11 12
17 18 19 20
I made a matrix in Matlab, say,
A = magic(5);
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Now I found the indices I want using the find function as:
ind = find(A(:,5)>3 & A(:,4)>= 8);
ind =
1
2
3
Now if I want to get a subset of matrix A for those indices using B = A(ind) function, I only get the first column of the matrix:
B = A(ind)
B =
17
23
4
How can I get all the columns as subset??
Oops ... I got it
B = A(ind,:);
I have a <1 x 29> cell array. Within each cell there is a <310x2000 double> matrix. Because of memory issues I would like to break these matrices up into smaller "chunks" (let's say approx. 5 x 2000 each) starting at row 4 and ending at row 309.
My data is therefore stored in the format of data{i}(j,:) where i refers to the cell number, and j refers to the desired row.
I created a matrix (let's call it A) in which each column gives me the values of the rows I want in each "chunk"
4 10 15 20 25
5 11 16 21 26
6 12 17 22 27 ...
7 13 18 23 28
8 14 19 24 29
For example, matrix 1 will include rows 4, 5, 6, 7 and 8.
nCells refers to the number of cells I have (29) & nCol refers to the number of columns in matrix A. Therefore:
for i = 1:nCells
for k = 1:nCol
for j = A(:,k)
B{i,k}(j,:) = [data{i}(j,:)];
end
end
end
Unfortunately this gives me the following error:
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
I really appreciate it if someone could tell me what the problem is or/ESPECIALLY if there's a much better way of going about doing this because I run into memory problems:
??? Out of memory. Type HELP MEMORY for your options.
First, the error you are getting is a result of how for loop indices are taken out of an array. In your third nested loop, you are setting j to a column vector. If you replaced this with A(:,k)', then it would probably work. See the following demonstration:
>> for i=([1;2;3;4]), disp(['i=' int2str(i)]); end;
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
>> for i=([1,2,3,4]), disp(['i=' int2str(i)]); end;
i=1
i=2
i=3
i=4
Second, you might want to do this a different way. My first instinct is to make a function such as this:
>> splitcell = #(C,range)(cellfun( #(X)(X(range,:)), C, 'UniformOutput', false) )
This uses the excellent cellfun command to call a function on each element of a cell array. Here, we are using matlab's anonymous function syntax (the # part) to make a function that takes in a matrix and slices it with a given range. You can make splitcell a normal function in its own m-file, I just used # here for compactness. splitcell has the following usage:
>> data = cell(1,5);
>> X = [1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16]
X =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
>> for i=1:5, data{i} = (X+i*ones(4)); end;
>> data{1:2}
ans =
2 3 4 5
6 7 8 9
10 11 12 13
14 15 16 17
ans =
3 4 5 6
7 8 9 10
11 12 13 14
15 16 17 18
>> data2 = splitcell(data,3:4)
data2 =
[2x4 double] [2x4 double] [2x4 double] [2x4 double] [2x4 double]
>> data2{1:2}
ans =
10 11 12 13
14 15 16 17
ans =
11 12 13 14
15 16 17 18