looping through multiple numbers

I have written a script, which tells me which rows of A are above the threshold, and then the difference between the numbers in column 1 of the variable 'abovethresh'.
This works fine, however I now want to write a loop which gives me this output, for avsize = 1:1:45. So I want it to apply the new threshold and do the same thing. I've tried to get a small section of this working to build on but I am getting the error below.
%single value
avsize = 10; %define avalanche threshold
abovethresh = A(A(:, 2) > 10,:);
wait_time = diff(abovethresh(:,1));
wait_time = wait_time(wait_time >15,:);
%loop
avsize = [1:1:45]
for i = 1:length(avsize)
a_t(i) = A(A(:,2) > avsize,:);
end
The logical indices in position 1 contain a true value outside of the array bounds.

 採用された回答

Walter Roberson
Walter Roberson 2022 年 5 月 26 日

0 投票

a_t(i) = A(A(:,2) > avsize(i),:);

6 件のコメント

C.G.
C.G. 2022 年 5 月 26 日
Thank you, can I ask a follow up question?
I am trying to save the output of every iteraton of w_t. I know I need to index this as w_t(i), but when I do this I get the error:
A = [t Q];
avsize = [1:1:5]; %define range of avalanche thresholds
for i = 1:length(avsize)
a_t = A(A(:,2) > avsize(i),:); %find the rows above each threshold
w_t = diff(a_t(:,1));
w_t(i) = w_t(w_t >15,:);
end
Unable to perform assignment because the left and right sides have a different number of elements.
Torsten
Torsten 2022 年 5 月 26 日
編集済み: Torsten 2022 年 5 月 26 日
W_t{i} = w_t(w_t >15,:);
C.G.
C.G. 2022 年 5 月 26 日
I get the following error by implementing that line:
Unable to perform assignment because brace indexing is not supported for variables of this type.
Walter Roberson
Walter Roberson 2022 年 5 月 26 日
w_t = diff(a_t(:,1));
w_t(i) = w_t(w_t >15,:);
You create w_t as a numeric column vector. You take a subset of it on the next line. You try to assign the subset to the single location in the same vector, w_t(i) which is a problem if the subset has more than one element.
If by chance you succeed on the assignment, then the next iteration you overwrite all of w_t
C.G.
C.G. 2022 年 5 月 26 日
編集済み: C.G. 2022 年 5 月 26 日
so are you saying I need to change the name of w_t(i) so it is not the same as the line above?
If i do that I get exactly the same error message.
Walter Roberson
Walter Roberson 2022 年 5 月 26 日
all_w_t{i} = w_t(w_t >15,:);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeElementary Math についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by