Combining matrices

5 次查看(过去 30 天)
Paul
Paul 2011-11-9
编辑: Paul 2015-12-2
I'm trying to build a matrix with the number of occurences of a given condition from two matrices.
The objective is to now the number of points that correspond to a given statement and combining this into a m*n matrix to use on a plot
For example: how many waves have period<0.5 and height<1, how many waves have period<0.5 and 1<height<1.5. etc...
my class intervals are period=[0:1:17] and height=[0:0.5:10].
Is there a way to build a function that would in some way combine the two classes into a matrix that would help me build something like (I'm only interested in the numbers):
t=[period H] x=matrix size(t) %with the number of occurences of each class.
I tried using multiple for and if conditions and even simpler ways, but I'm feeling really lost.
Can someone help me?
thank you in advance
Paul

采纳的回答

Paul
Paul 2011-11-9
Basically, I want to do something like:
intervalHs=[0:0.5:10];
intervalTe=[0:1:20];
a=sum((Hs>0.5)&(Hs<1)&(Te<=1))
b=sum((Hs>2)&(Hs<=2.5)&(Te<=6)&(Te>5))
x=sum((Hs<=0.5)&(Te<=1))
y=sum((Hs<=0.5)&(Te>1)&(Te<=2))
z=sum((Hs<=0.5)&(Te>2)&(Te<=3))
and the construct a matrix (m*n) with the results

更多回答(2 个)

John Petersen
John Petersen 2011-11-9
Paul, Try find() or a straight comparison (if you don't need to know the indices). For example,
function [num_periods, num_heights] = findnum(period,plowvalue,phighvalue,height,hlowvalue,hhighvalue)
num_periods = sum((period>plowvalue)&(period<phighvalue));
num_heights = sum((height>hlowvalue)&(height<hhighvalue));
  1 个评论
Paul
Paul 2011-11-9
First, thanks for the answer.
I did tried to do something like that. Unfortunately that would mean I would have to insert all the possible combinations between the two classes one by one.
The idea is to count the number of waves that fall into both classes at once for each class interval (I'm finding some trouble in explaining).
For example: in the figure the value that corresponds to the number of waves that have a period between 5 and 6 s and a height between 1.5 and 2 is 584.
I know there must be a easy way of doing so, but I can't put my finger on it!

请先登录,再进行评论。


Paul
Paul 2015-12-2
编辑:Paul 2015-12-2
THIS IS THE ACCEPTED ANSWER!!!!!!!!!!!!!!!!
I can't seem to undo my previou accepted answer....
y=0:1:20; %Te interval
x=0:0.5:10; %Hs interval
Hs=seccao1(:,2);
Tm=seccao1(:,3);
for n=1:20
Tm_id=Tm(Hs>x(n) & Hs<=x(n+1));
for k=1:20
s=sum(Tm_id>y(k) & Tm_id<=y(k+1));
M(n,k)=s; % M is a matrix with the number of occurences of each given interval (i.e: number of waves with %1<=Hs <1.5 and 5<=Te<6)
end
end

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by