How to search for different values in a 2-D Array using a for loop
    6 ビュー (過去 30 日間)
  
       古いコメントを表示
    
I have been given this question:

I have written this code below but it doesn't seem to work it skips all the iff statments and im not sure why. The array in which i am inputing does have values which should create results. Anyone know what i am doing wrong??
function [wh_1, wh_3, wh_5, wh_7, wh_9, wh_11, wh_13] = count_table(table, col, row)
    for m = 1:col
        for n = 1:row
            if isequal(table(n,m), 0)
                wh_1 = wh_1 + 1;
            elseif isequal(table(n,m), 1)
                wh_1 = wh_1 + 1;
            elseif isequal(table(n,m), 2)
                wh_3 = wh_3 + 1;
            elseif isequal(table(n,m), 3)
                wh_3 = wh_3 + 1;
            elseif isequal(table(n,m), 4)
                wh_5 = wh_5 + 1;
            elseif isequal(table(n,m), 5)
                wh_5 = wh_5 + 1;
            elseif isequal(table(n,m), 6)
                wh_7 = wh_7 + 1;
            elseif isequal(table(n,m), 7)
                wh_7 = wh_7 + 1;
            elseif isequal(table(n,m), 8)
                wh_9 = wh_9 + 1;
            elseif isequal(table(n,m), 9)
                wh_9 = wh_9 + 1;
            elseif isequal(table(n,m), 10)
                wh_11 = wh_11 + 1;
            elseif isequal(table(n,m), 11)
                wh_11 = wh_11 + 1;
            elseif isequal(table(n,m), 12)
                wh_13 = wh_13 + 1;
            elseif isequal(table(n,m), 13)
                wh_13 = wh_13 + 1;
            end
        end
    end
end
Here is the error in which is produced:
            Output argument "wh_1" (and maybe others) not assigned during call to "count_table".
            Error in  (line 44)
            [z_1, z_3, z_5, z_7, z_9, z_11, z_13] = count_table(wave_data, 60, 498);
1 件のコメント
  Stephen23
      
      
 2020 年 3 月 30 日
				The cause of the error is that none of the variables wh_1, etc. exist before you try to reference them:
wh_1 = wh_1 + 1;
%      ^^^^ is not defined.
However the entire code is far too complex for this task.
Numbered variables are usually a sign that something is wrong: most likely they should be replaced with one variable.
採用された回答
  Shishir Singhal
      
 2020 年 4 月 9 日
        
      編集済み: darova
      
      
 2020 年 4 月 9 日
  
      Please declare all variables like wh_1, wh_3 and set them to 0 at the beginning of your code. It should work fine.
And for specifiying conditions, you can also try like 
if(table(n,m) >=0 && table(n, m)<=1)
    wh_1 = wh_1+1;
elseif(table(n, m)<=2 && table(n, m)<=3)
    wh_3=wh_3+1;
and so on.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
				Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!