필터 지우기
필터 지우기

Several Operations Within a Matrix Matlab

조회 수: 1 (최근 30일)
sami elahj
sami elahj 2016년 1월 10일
댓글: Guillaume 2016년 1월 10일
i'd like someone with knowledge on matlab to help me sort this out, the plan is to write a script as follow
it starts reading numbers on column 2 and check in column 3 one if they are repeated: The Condition is, if a number in column 2 is repeated more than one time in column 3, the corresponding values in column 4 will be summed up, if a number from column 2 is not repeated or only repeated one time in column 3, its corresponding value in column 4 will be left as it is. The obtained values would be generated in a vector. the letters were put there just for explanation here is an example to try with
data=[0 1 0 0
1 2 1 200
2 3 1 300
3 4 1 400
4 5 2 500
5 6 3 600
6 7 4 700
7 8 5 800
8 9 5 900
9 10 5 1000
10 11 6 1100
11 12 7 1200
12 13 7 1300];
this would be the desired result
v=[(200+300+400) 200 300 400 (800+900+1000) 600 (1200+1300) 800 900 1000 1100 1200 1300]
waiting on any ideas.Thank you programmers !

채택된 답변

Stephen23
Stephen23 2016년 1월 10일
편집: Stephen23 2016년 1월 10일
data=[0 1 0 0
1 2 1 200
2 3 1 300
3 4 1 400
4 5 2 500
5 6 3 600
6 7 4 700
7 8 5 800
8 9 5 900
9 10 5 1000
10 11 6 1100
11 12 7 1200
12 13 7 1300];
D = diff(data(2:end,3),1);
E = [false;~D] | [~D;false];
tmp = cumsum(diff([false;E])>0);
out = data(:,4);
idx = data([false;diff([false;E])>0],3);
out(idx) = accumarray(tmp(E),data([false;E],4));
And this code's output compared to your example output:
>> [out,v]
ans =
900 900
200 200
300 300
400 400
2700 2700
600 600
2500 2500
800 800
900 900
1000 1000
1100 1100
1200 1200
1300 1300
  댓글 수: 1
sami elahj
sami elahj 2016년 1월 10일
Stephen your code worked for thank you :)

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

추가 답변 (2개)

dpb
dpb 2016년 1월 10일
Doesn't look to me like the result is really dependent upon the third column at all; it's simply accumulating the like values in the fourth....
>> v=accumarray(data(:,3)+1,data(:,4))
v =
0
900
500
600
700
2700
1100
2500
>>
*NB: * the need to add one to the column values in column three to be valid array indices of positive integers.
  댓글 수: 1
sami elahj
sami elahj 2016년 1월 10일
>> v =
900
200
300
400
2700
600
2500
800
900
1000
1100
1200
1300
hey dpb this would be the desired result, i don't understand your note at the end

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


Guillaume
Guillaume 2016년 1월 10일
편집: Guillaume 2016년 1월 10일
Your output vector is really weird. Are you sure that's really what you want?
Ir may be possible to achieve it without a loop but the resulting code may be very obscure. Anyway, here is one way of doing it:
[values, ~, subs] = unique(data(:, 3)); %it's much safer to use unique than adding 1 to dat(:, 3)
valsum = accumarray(subs, data(:, 4)); %since it works regardless of the range of values
count = accumarray(subs, 1);
v = zeros(size(data(:, 3)));
for row = 1:numel(v)
valrow = values == data(row, 2); %empty if data(row, 2) is not found in column 3
if count(valrow) > 1 %note that empty>1 returns false, so this works properly
v(row) = valsum(valrow);
count(valrow) = 1; %prevents current valsum to be included again.
else
v(row) = data(row, 4);
end
end
  댓글 수: 2
sami elahj
sami elahj 2016년 1월 10일
@Guillaume i tried your code but i got this thanx anyway
Guillaume
Guillaume 2016년 1월 10일
An obvious typo that you should have spotted. Replace counts by count.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by