Hello, I have a large matrix with 3 columns(x,y,and z) and I want to rearrange the values inside this matrix. Example:
*Initial matrix
x y z
1 1 5
1 2 6
1 3 7
2 1 8
2 2 9
2 3 2
*Final matrix My goal is to rearrange the values of the matrix in this form
y
x 1 2 3
1 5 6 7 <-z values
2 8 9 2

 채택된 답변

Sean de Wolski
Sean de Wolski 2013년 11월 4일

0 개 추천

There are a few elegant ways to do this. I would use accumarray
data = [
1 1 5
1 2 6
1 3 7
2 1 8
2 2 9
2 3 2];
V = accumarray(data(:,[1 2]),data(:,3))
Now V does not have the index values, but you already know them:
rr = 1:size(V,1) %row
cc = 1:size(V,2) %column
So I would just keep them separate.

댓글 수: 5

afrya
afrya 2013년 11월 4일
Thanks for your answer, could you tell me an another way to rearraenge the values and keeping the index values inside the matrix?
Sean de Wolski
Sean de Wolski 2013년 11월 4일
편집: Sean de Wolski 2013년 11월 4일
See Andrei's answer below.
Or add to mine:
V = [[nan cc];[rr(:),V]]]
afrya
afrya 2013년 11월 4일
Thanks
When I'm working with decimal numbers,I have the following error
x =
0.6000 0.6000 5.0000
0.6000 0.8000 6.0000
0.6000 0.9000 7.0000
0.8000 0.6000 8.0000
0.8000 0.8000 9.0000
0.8000 0.9000 2.0000
>> V = accumarray(x(:,[1 2]),x(:,3)) Error using accumarray First input SUBS must contain positive integer subscripts.
Do you know how to solve this problem?
Andrei Bobrov
Andrei Bobrov 2013년 11월 4일
see my answer

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2013년 11월 4일
편집: Andrei Bobrov 2013년 11월 4일

0 개 추천

xyz = [1 1 5
1 2 6
1 3 7
2 1 8
2 2 9
2 3 2];
out = nan(max(xyz(:,1:2))+1);
out(2:end,1) = unique(xyz(:,1));
out(1,2:end) = unique(xyz(:,2));
out(2:end,2:end) = accumarray(xyz(:,1:2),xyz(:,3));
ADD
x =[ 0.6000 0.6000 5.0000
0.6000 0.8000 6.0000
0.6000 0.9000 7.0000
0.8000 0.6000 8.0000
0.8000 0.8000 9.0000
0.8000 0.9000 2.0000];
[r,ii,ii] = unique(x(:,1));
[c,jj,jj] = unique(x(:,2));
v = accumarray([ii,jj],x(:,3));
out = [nan,c';r,v];

댓글 수: 6

afrya
afrya 2013년 11월 4일
Thanks for your answer
Hello,
I'm working with a large matrix that contains a lot of zero values.When I transform this matrix from a list table to a cross table, I have more zero values in it.I want to keep all the initial zeros values in it and supress all the zero values added from matlab for the calculation of the average.Here is an example of what I want to do:
x=
0.6000 0.6000 5.0000
0.6000 0.8000 6.0000
0.6000 0.8500 0
0.6000 0.8200 0
0.8000 0.8000 9.0000
0.8000 0.9000 2.0000
List table to cross table
NaN 0.6000 0.8000 0.8200 0.8500 0.9000
0.6000 5.0000 6.0000 0 0 0 Average1
0.8000 0 9.0000 0 0 2.0000 Average2
Average1=(5+6+0+0)/4=2,75
Average2=(9+2)/2=5,5 Thanks in advance
x=[ 0.6000 0.6000 5.0000
0.6000 0.8000 6.0000
0.6000 0.8500 0
0.6000 0.8200 0
0.8000 0.9000 2.0000
0.8000 0.8000 9.0000];
[rc,~,c1] = arrayfun(@(ii)unique(x(:,ii)),1:2,'un',0);
v = accumarray([c1{:}],x(:,3));
out = [nan,rc{2}',nan;rc{1},v,sum(v,2)./sum(v>0,2)];
or other last row
out = [nan,rc{2}',nan;rc{1},v,sum(v,2)./numel(rc{2})];
afrya
afrya 2014년 4월 5일
Thank you for your answer,it helped me. Now, what I want to do is to calculate the average for each row but without taking into account the zeros added by matlab.
1st row: there is only one zero added by matlab for this coordinate(0.6;0.9) So , i would like to calculate this average: Average1=(5+6+0+0)/4=2.75
2nd row: there is 3 zeros added by matlab for this coordinates(0.8;0.6),(0.8;0.82),(0.8;0.85) so i'd like to calculate this average, Average2=(9+2)/2=5.5
I have no idea how to do that knowing that i'm working with a large matrix, thanks in advance
another variant of last row
out = [nan,rc{2}',nan;rc{1},v,accumarray(c1{1},x(:,3),[],@mean)];
afrya
afrya 2014년 4월 6일
Thanks for your help, it works now

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2013년 11월 4일

댓글:

2014년 4월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by