Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
help with find

Subject: help with find

From: Jonathan

Date: 7 Jun, 2010 05:46:04

Message: 1 of 4

G'day,

I have the following script...

data = load('arago_winds.txt'); % 201,600*2 matrix
time_wnd = data(:,1); % length is 201600

ddata = load(storms); % 134*6 matrix
time_events=ddata(:,1);

inds = zeros(size(time_wnd)); % preallocate
for i=1:size(time_wnd);
    for j=1:size(time_events);
        inds(i,j) = find(time_events(i)==time_wnd(j));
    end
end

I am trying to find those specific events identified by 'time_events' (134 events) that match the same time values identified by time_wnd (hourly values spanning 28 years). The purpose is to locate specific wind speed values that match the events. I am getting a dimension mismatch and I'm not sure what I am doing wrong. Any help would be much appreciated.

Thanks
Jon

Subject: help with find

From: Walter Roberson

Date: 7 Jun, 2010 06:29:56

Message: 2 of 4

Jonathan wrote:

> inds(i,j) = find(time_events(i)==time_wnd(j));

> I am getting a dimension
> mismatch and I'm not sure what I am doing wrong.

In general, find() will return multiple entries if there are multiple
matches of the condition. Multiple entries cannot, however, be stored in
a single array element such as inds(i,j) . If you need to store multiple
indices, then use a cell array, inds{i,j}; if you only want to find the
first match, use

find(time_vents(i)==time_wnd(j),1)

If you only want to find the last match, use


find(time_vents(i)==time_wnd(j),1,'last')

Subject: help with find

From: Steven Lord

Date: 7 Jun, 2010 13:45:13

Message: 3 of 4


"Walter Roberson" <roberson@hushmail.com> wrote in message
news:El0Pn.52843$h57.32444@newsfe22.iad...
> Jonathan wrote:
>
>> inds(i,j) = find(time_events(i)==time_wnd(j));
>
>> I am getting a dimension mismatch and I'm not sure what I am doing wrong.
>
> In general, find() will return multiple entries if there are multiple
> matches of the condition. Multiple entries cannot, however, be stored in a
> single array element such as inds(i,j) . If you need to store multiple
> indices, then use a cell array, inds{i,j}; if you only want to find the
> first match, use

While that's true, that's not the source of the error in this case; indeed,
the reverse problem is the cause of the dimension mismatch error. In
Jonathan's code, i and j are FOR loop variables and so this is calling FIND
on a scalar logical. If the scalar logical is true, 1 is stored in the
element inds(i, j). If the scalar logical is false, FIND returns an empty
array and you can't "fit" an empty array into a scalar element of a
matrix -- the sizes don't match.

Jonathan, either get rid of the FIND call:

inds(i, j) = time_events(i) == time_wnd(j);

or get rid of the FIND call and one of your loops:

inds = zeros(numel(time_events), numel(time_wnd));
time_wnd = time_wnd(:); % make sure it's a column vector
for row = 1:numel(time_events)
    inds(row, :) = time_events(row) == time_wnd;
end

or get rid of the FOR loops entirely:

time_events = time_events(:); % column vector
time_wnd = time_wnd(:).'; % row vector
inds = bsxfun(@eq, time_events, time_wnd);

Also, the usual caveats about performing equality checking on floating-point
numbers apply; see Q6.1 in the newsgroup FAQ if you're not familiar with
those concerns.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: help with find

From: Jonathan

Date: 7 Jun, 2010 16:25:05

Message: 4 of 4

Thanks for your help. I'm now not sure I am going about this the right way now. I followed your suggestions. Here's the code...

data = load('arago_winds.txt'); % load hourly wind data
time_wnd = load('arago_winds_time.txt'); % hourly time data (matlab format)
ddata = load('events.txt'); % load storm event file
time_events=ddata(:,1); % time of specific events

inds = zeros(numel(time_events), numel(time_wnd)); % preallocate
time_wnd = time_wnd(:); % make sure it's a column vector
for row = 1:numel(time_events)
    inds(row, :) = time_events(row) == time_wnd;
end

So the thinking here was to use 'inds' to find those specific time_events that equalled the associated hourly wind event times and then eliminate everyhting except for just 134 wind values that I am trying to find. However, the final matrix is 201600*134. So I then tried:

a = max(inds).'; % inds produces a 201600*134 matrix. consolidate to 201600*1 % %(i.e. zeros (no event) and ones (event).
inds = find(a==0); % find the zeros
data(inds) = []; % eliminate zeros and identify specific wind speeds associated with % time_events.

However, this does not seem to do what I want. What am I doing wrong?

Thanks
Jon

"Steven Lord" <slord@mathworks.com> wrote in message <huit4k$lt1$1@fred.mathworks.com>...
>
> "Walter Roberson" <roberson@hushmail.com> wrote in message
> news:El0Pn.52843$h57.32444@newsfe22.iad...
> > Jonathan wrote:
> >
> >> inds(i,j) = find(time_events(i)==time_wnd(j));
> >
> >> I am getting a dimension mismatch and I'm not sure what I am doing wrong.
> >
> > In general, find() will return multiple entries if there are multiple
> > matches of the condition. Multiple entries cannot, however, be stored in a
> > single array element such as inds(i,j) . If you need to store multiple
> > indices, then use a cell array, inds{i,j}; if you only want to find the
> > first match, use
>
> While that's true, that's not the source of the error in this case; indeed,
> the reverse problem is the cause of the dimension mismatch error. In
> Jonathan's code, i and j are FOR loop variables and so this is calling FIND
> on a scalar logical. If the scalar logical is true, 1 is stored in the
> element inds(i, j). If the scalar logical is false, FIND returns an empty
> array and you can't "fit" an empty array into a scalar element of a
> matrix -- the sizes don't match.
>
> Jonathan, either get rid of the FIND call:
>
> inds(i, j) = time_events(i) == time_wnd(j);
>
> or get rid of the FIND call and one of your loops:
>
> inds = zeros(numel(time_events), numel(time_wnd));
> time_wnd = time_wnd(:); % make sure it's a column vector
> for row = 1:numel(time_events)
> inds(row, :) = time_events(row) == time_wnd;
> end
>
> or get rid of the FOR loops entirely:
>
> time_events = time_events(:); % column vector
> time_wnd = time_wnd(:).'; % row vector
> inds = bsxfun(@eq, time_events, time_wnd);
>
> Also, the usual caveats about performing equality checking on floating-point
> numbers apply; see Q6.1 in the newsgroup FAQ if you're not familiar with
> those concerns.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
>

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us