Find, sort and assign values in matrix
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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 个评论
更多回答(2 个)
KALYAN ACHARJYA
2020-12-4
data=q3(:,4)/3;
data=[data(1);data(1:end-1)+data(2:end)];
q4=[q3(:,1),data]
0 个评论
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]
... more rows here
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!