# Get all pairs from a 2D matrix in columns (CSV)

2 Ansichten (letzte 30 Tage)
jose sanchez am 3 Apr. 2020
Beantwortet: Jalaj Gambhir am 6 Apr. 2020
Given a matrix 3x4 like this one:
a = [ 12 34 25 23
76 12 8 59
23 56 89 61]
I would like to export it to a csv file like this (including the first 2 columns as row x column indexes from 2 other matrixes):
rows = [56 12 90]
columns = [4 2 8 1]
CSV:
56,4,12
56,2,34
56,8,25
56,1,23
12,4,76
..
I'm new to matlab, any ideas?
I have tried this:
A = A([1:3 1:end]);
A = permute(A,[2 1]);
csvwrite("test.csv",A);
Which works for matrix A, but I'm unable so far to put together the other 2 matrixes into columns 1, 2 as in the example above.
Thanks in advance,
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Jalaj Gambhir am 6 Apr. 2020
Hi,
This can be achieved as follows:
[m,n] = ndgrid(rows,columns);
row_col = [m(:),n(:)];
Here row_col results in:
row_col =
56 4
12 4
90 4
56 2
12 2
. .
. .
Then, you can flatten your array 'a' row-wise using:
flat_array = reshape(a.',1,[]);
Finally, concatenate the 'row_col' and 'flat_array' horizontally.
final_result = horzcat(row_col, flat_array);
Result obtained:
56 4 12
12 4 34
90 4 25
56 2 23
12 2 76
. . .
. . .
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

R2019b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by