フィルターのクリア

looping for a function

1 回表示 (過去 30 日間)
Nurul Ain Basirah Zakaria
Nurul Ain Basirah Zakaria 2021 年 6 月 23 日
コメント済み: Walter Roberson 2021 年 6 月 24 日
Hi, can I apply
[output1, output2] = calculate(b);
in a 3D data such as 80x30x400 data?
  2 件のコメント
KSSV
KSSV 2021 年 6 月 23 日
It depends on your function calculate.
Nurul Ain Basirah Zakaria
Nurul Ain Basirah Zakaria 2021 年 6 月 23 日
is it arrayfun?

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

回答 (2 件)

KSSV
KSSV 2021 年 6 月 23 日
This is what you are looking for?
[m,n,p] = size(b) ; % b is matrix of size 80x30x400
iwant = zeros(p,2);
for i = 1:p
[output1, output2] = calculate(b(:,:,i));
iwant(i,:) = [output1, output2] ;
end
  6 件のコメント
Nurul Ain Basirah Zakaria
Nurul Ain Basirah Zakaria 2021 年 6 月 23 日
but i already add to path, when i run it for a single data or vector, it works fine. But, in order to run it in 3D (84x36x400) data, it comes out as:
for i=1:84
for j=1:36
[freq, duration, peak, severity]=drought(3D(i,j,:));
end
end
Output argument "duration" (and maybe others) not assigned during call to "drought".
KSSV
KSSV 2021 年 6 月 23 日
There is no output called duration in drought.

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


Walter Roberson
Walter Roberson 2021 年 6 月 23 日
This has nothing to do with a missing function.
Your code in drought.m is defined to expect at least four output variables -- otherwise you would have received an error about too many output arguments before you reached the error you did.
Your code in drought.m assigns some value to the first of the output variables it assigns.
However, your code in drought.m does not assign any value to the second of the output variables it assigns.
At the moment we do not have enough information to know whether drought.m never assigns to the second output variable, or if instead it does not assign to the second output variable under some conditions and the conditions happen to be such that it does not assign.
For example,
function [A, B, C] = drought(data)
if any(data(:) < 0 %negative rainfall ??
A = nan; %signal invalid data
return
end
A = mean(data);
B = std(data);
c = var(data);
end
In this example code, B is not assigned to if there are any negative values in the input, and C is never assigned to under any conditions. In this example code it could well be the case that the intention was to assign to C instead of c, but MATLAB doesn't know anything about intentions.
  2 件のコメント
Nurul Ain Basirah Zakaria
Nurul Ain Basirah Zakaria 2021 年 6 月 23 日
but if i apply it for single data, i got the outcomes, but this problem appeared when i try to calculate for 3D data.
Walter Roberson
Walter Roberson 2021 年 6 月 24 日
We have not seen your function drought so it is difficut to say why it is failing.
Could you confirm that it is failing when the input is a 1 x 1 x Something array (such as 1 x 1 x 400) ?

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by