Find, sort and assign values in matrix

조회 수: 4 (최근 30일)
Simon Kibsgaard
Simon Kibsgaard 2020년 12월 4일
댓글: Simon Kibsgaard 2020년 12월 11일
Hi im very new to matlab and cant get the following input to find and sort correctly.
An help is much appereciated :)
%input
q3 =
1.0000 2.0000 90.0000 4.9424
2.0000 3.0000 91.0000 4.3018
3.0000 4.0000 92.0000 3.7912
4.0000 5.0000 93.0000 3.4618
5.0000 6.0000 94.0000 3.3465
6.0000 7.0000 95.0000 3.4566
7.0000 8.0000 96.0000 3.7815
8.0000 9.0000 97.0000 4.2892
9.0000 10.0000 98.0000 4.9293
10.0000 11.0000 99.0000 5.6382
...
%wanted output
q4=
1.0000 (4.9424/3)
2.0000 (4.9424/3)+(4.3018/3)
3.0000 (4.3018/3)+(3.7912/3)
4.0000 (3.7912/3)+(3.4618/3)
5.0000 (3.4618/3)+(3.3465/3)
6.0000 (3.3465/3)+(3.4566/3)
...
98.0000 (4.9293/3)
99.0000 (5.6382/3)
The numbers is column 1, 2 and 3 are the numbers of my nodes. Each row is the nodes of a triangle and the area of that triangle.
Now need the area of each triangle to be divided out onto each node of that triangle. At last i need all of the areas that are divided onto node number 1 to be sumed up and so forth for each node.
  댓글 수: 1
KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 12월 4일
data=q3(:,4)/3;
data=[data(1);data(1:end-1)+data(2:end)];
q4=[q3(:,1),data]

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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 12월 4일
편집: Ameer Hamza 2020년 12월 4일
Are you trying to get something like this
q3 = [
1.0000 2.0000 90.0000 4.9424
2.0000 3.0000 91.0000 4.3018
3.0000 4.0000 92.0000 3.7912
4.0000 5.0000 93.0000 3.4618
5.0000 6.0000 94.0000 3.3465
6.0000 7.0000 95.0000 3.4566
7.0000 8.0000 96.0000 3.7815
8.0000 9.0000 97.0000 4.2892
9.0000 10.0000 98.0000 4.9293
10.0000 11.0000 99.0000 5.6382];
q_ = q3(:,1:3);
vals1 = unique(q_);
vals2 = zeros(size(vals1));
for i = 1:numel(vals1)
idx = any(q_==vals1(i),2);
vals2(i) = sum(q3(idx,4));
end
q4 = [vals1 vals2/3];
Result
>> q4
q4 =
1.0000 1.6475
2.0000 3.0814
3.0000 2.6977
4.0000 2.4177
5.0000 2.2694
6.0000 2.2677
7.0000 2.4127
8.0000 2.6902
9.0000 3.0728
10.0000 3.5225
11.0000 1.8794
90.0000 1.6475
91.0000 1.4339
92.0000 1.2637
93.0000 1.1539
94.0000 1.1155
95.0000 1.1522
96.0000 1.2605
97.0000 1.4297
98.0000 1.6431
99.0000 1.8794
  댓글 수: 3
Ameer Hamza
Ameer Hamza 2020년 12월 4일
I am glad to be of help!
Simon Kibsgaard
Simon Kibsgaard 2020년 12월 11일
Hi Again
Now i have an other issue sorting. Hope you can help me again.
I have the data as shown below.
The values in the first column are my node numbers and the values in the third column are the forces in each node. The second column show the dimension of the force.
I need all of the forces to be summed up, but the dimensions cant be mixed. i have also attached a example of the output f that i wish for. My data set is much bigger and therefore ii cant be done by hand.
30 3 -1.61e+04
31 3 -1.83e+04
32 3 -2.09e+04
33 3 -7.53e+03
89 3 -1.5e+04
90 3 -4.14e+04
91 3 -3.62e+04
92 3 -3.18e+04
93 3 -2.88e+04
94 3 -2.73e+04
95 3 -2.76e+04
96 3 -2.96e+04
97 3 -3.32e+04
98 3 -3.79e+04
99 3 -2.82e+04
30 3 -1.05e+03
31 3 -1.2e+03
32 3 -1.37e+03
33 3 -493
89 3 -986
90 3 -2.71e+03
91 3 -2.37e+03
92 3 -2.08e+03
93 3 -1.88e+03
94 3 -1.79e+03
95 3 -1.81e+03
96 3 -1.94e+03
97 3 -2.17e+03
98 3 -2.48e+03
99 3 -1.85e+03
1 1 1.43e+03
2 1 1.91e+03
3 1 1.67e+03
4 1 1.49e+03
5 1 1.38e+03
6 1 1.36e+03
7 1 1.42e+03
8 1 1.57e+03
9 1 1.79e+03
10 1 2.05e+03
11 1 752
23 1 1.39e+03
24 1 1.88e+03
25 1 1.65e+03
26 1 1.47e+03
27 1 1.38e+03
28 1 1.37e+03
29 1 1.45e+03
30 1 1.61e+03
31 1 1.83e+03
32 1 2.09e+03
33 1 753
89 1 1.5e+03
90 1 4.14e+03
91 1 3.62e+03
92 1 3.18e+03
93 1 2.88e+03
94 1 2.73e+03
95 1 2.76e+03
96 1 2.96e+03
97 1 3.32e+03
98 1 3.79e+03
99 1 2.82e+03
f=
30 3 -1.61e+04+(-1.05e+03)
31 3 -1.83e+04+(-1.2e+03)
32 3 -2.09e+04+(-1.37e+03)
33 3 -7.53e+03+(-493)
89 3 -1.5e+04+(-986)
90 3 -4.14e+04+(-2.71e+03)
91 3 -3.62e+04+(-2.37e+03)
92 3 -3.18e+04+(-2.08e+03)
93 3 -2.88e+04+(1.88e+03)
94 3 -2.73e+04+(-1.79e+03)
95 3 -2.76e+04+(-1.81e+03)
96 3 -2.96e+04+(-1.94e+03)
97 3 -3.32e+04+(-2.17e+03)
98 3 -3.79e+04+(-2.48e+03)
99 3 -2.82e+04+(-1.85e+03)
1 1 1.43e+03
2 1 1.91e+03
3 1 1.67e+03
4 1 1.49e+03
5 1 1.38e+03
6 1 1.36e+03
7 1 1.42e+03
8 1 1.57e+03
9 1 1.79e+03
10 1 2.05e+03
11 1 752
23 1 1.39e+03
24 1 1.88e+03
25 1 1.65e+03
26 1 1.47e+03
27 1 1.38e+03
28 1 1.37e+03
29 1 1.45e+03
30 1 1.61e+03
31 1 1.83e+03
32 1 2.09e+03
33 1 753
89 1 1.5e+03
90 1 4.14e+03
91 1 3.62e+03
92 1 3.18e+03
93 1 2.88e+03
94 1 2.73e+03
95 1 2.76e+03
96 1 2.96e+03
97 1 3.32e+03
98 1 3.79e+03
99 1 2.82e+03

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

추가 답변 (2개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 12월 4일
data=q3(:,4)/3;
data=[data(1);data(1:end-1)+data(2:end)];
q4=[q3(:,1),data]

Stephen23
Stephen23 2020년 12월 4일
편집: Stephen23 2020년 12월 4일
q3 = [
1.0000 2.0000 90.0000 4.9424
2.0000 3.0000 91.0000 4.3018
3.0000 4.0000 92.0000 3.7912
4.0000 5.0000 93.0000 3.4618
5.0000 6.0000 94.0000 3.3465
6.0000 7.0000 95.0000 3.4566
7.0000 8.0000 96.0000 3.7815
8.0000 9.0000 97.0000 4.2892
9.0000 10.0000 98.0000 4.9293
10.0000 11.0000 99.0000 5.6382];
[uni,~,idu] = unique(q3(:,1:3));
out = [uni,accumarray(idu,repmat(q3(:,4),3,1))/3]
out = 21×2
1.0000 1.6475 2.0000 3.0814 3.0000 2.6977 4.0000 2.4177 5.0000 2.2694 6.0000 2.2677 7.0000 2.4127 8.0000 2.6902 9.0000 3.0728 10.0000 3.5225
... more rows here

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by