フィルターのクリア

reading an array

2 ビュー (過去 30 日間)
Sayanta
Sayanta 2012 年 5 月 24 日
回答済み: Patel Keyur 2022 年 1 月 7 日
Hello All, I Have code below. I want to create an array with elements [ 5, 10, 15, 20,25, 30, 35, 40, 45, 50, 55, 60, 65, 75, 80, 85, 90, 95, 100] and I want replace if statement if (stateCount(n)> 5 && stateCount(n)<= 10) with value from an array 5 and 10. How can I do that. Any tips will gladly welcome
stateHit = zeros(length(stateCount),20);
for n = 1:length(stateCount)
if (stateCount(n)<= 5)
stateHit(n,1) = 1;
else
if (stateCount(n)> 5 && stateCount(n)<= 10)
stateHit(n,2) = 1;
else
if (stateCount(n)>10 && stateCount(n)<= 15)
stateHit(n,3) = 1;
else
if (stateCount(n)> 15 && stateCount(n)<= 20)
stateHit(n,4) = 1;
else
if (stateCount(n)> 20 && stateCount(n)<= 25)
stateHit(n,5) = 1;
else
if (stateCount(n)> 25 && stateCount(n)<= 30)
stateHit(n,6) = 1;
end
end
end
end
end
end
end
Many thanks in advance
SM

回答 (4 件)

Walter Roberson
Walter Roberson 2012 年 5 月 25 日
YourDividingArray = 5:5:100;
[bincounts, HistcIndex] = histc(StateCount, [-inf YourDividingArray(2:end)]);
StateHit( sub2ind(size(StateHit), HistcIndex, 1:size(StateHit,2)) ) = 1;
Or if you prefer,
HistcIndex = max(1, floor(StateCount / 5));
StateHit( sub2ind(size(StateHit), HistcIndex, 1:size(StateHit,2)) ) = 1;

Sayanta
Sayanta 2012 年 5 月 25 日
Hi Walter,
Thanks for your answer. I couldn't understand what your code will do, could you please explain, How can I implement it my code. Sorry I'm have elementary level in matlab
Thanks in advance
Sm

Sayanta
Sayanta 2012 年 5 月 25 日
Hi Walter, I use code like that but I have error
??? Undefined function or variable 'StateHit'.
Error in ==> short_code_test at 4 StateHit( sub2ind(size(StateHit), HistcIndex, 1:size(StateHit,2)) ) = 1;
Here is the code I try
StateCount = value; HistcIndex = max(1, floor(StateCount / 5)); StateHit( sub2ind(size(StateHit), HistcIndex, 1:size(StateHit,2)) ) = 1; disp(StateHit);
Thanks for help in advance
SM
  1 件のコメント
Walter Roberson
Walter Roberson 2012 年 5 月 25 日
Typo on my part. My StateHit should be stateHit to be consistent with your code. I assume here your line
stateHit = zeros(length(stateCount),20);

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


Patel Keyur
Patel Keyur 2022 年 1 月 7 日
How to read array in matlab and this array value store in any number device?

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by