Calculate values inside a loop and store them in variables.

I have a matrix and want to calculate where its values are above certain thresholds and store the results in a new matrix. My difficulty is that I want a different step, I tried a nested loop but the result was not the expected. Any ideas how to proceed?

 채택된 답변

Stephen23
Stephen23 2022년 12월 16일
편집: Stephen23 2022년 12월 16일
Assuming that SUM() returns a scalar:
V = [1:10,15:5:50];
C = V; % preallocate
for k = 1:numel(V) % loop over indices, not your data values
C(k) = sum(x>V(k));
end
Note that in MATLAB it is almost always easier to iterate over indices, rather than over data values.
Or alternatively:
C = arrayfun(@(v)sum(x>v), V)

추가 답변 (1개)

Jiri Hajek
Jiri Hajek 2022년 12월 8일
Hi, you can use any special vector of indexes (natual numbers) like this:
j = [1:10,15:5:50];
for i=j
C(i)= sum(x>i);
end

댓글 수: 6

"I expected values 18 values in total (1,2,3,4,5,6,7,8,9,10,15,20,25,30,35,40,45 and 50)."
V = [1:10,15:5:50]
V = 1×18
1 2 3 4 5 6 7 8 9 10 15 20 25 30 35 40 45 50
Strange, your answer makes absolute sense, yet I still receive 50 values.
Stephen23
Stephen23 2022년 12월 14일
편집: Stephen23 2022년 12월 14일
"Strange, your answer makes absolute sense, yet I still receive 50 values."
There is nothing strange going on at all: you are using the values of j as indices into C. The largest value of j is 50, so the largest index into C is 50, so C will be expanded to have 50 elements. The unassigned elements will have value zero, the default when arrays are expanded like that.
It might not be what you want (your request is totally unclear to me), but there is nothing strange going on here.
Stephen23
Stephen23 2022년 12월 15일
편집: Stephen23 2022년 12월 15일
"So V is 1X18 or 1X50? "
As far as I can tell, the basic issue is that both your question and this answer use data/parameter values as indices, whereas you really should be iterating over actual indices. You are confusing code and data. But you have already accepted this answer as answering your question, which tells everyone that you are happy with the results you get.
Let's take a smaller example, one where we can view the vector without scrolling.
indicesToAssignTo = [1 3 5]
indicesToAssignTo = 1×3
1 3 5
for whichInd = indicesToAssignTo
x(whichInd) = whichInd.^2
end
x = 1
x = 1×3
1 0 9
x = 1×5
1 0 9 0 25
When I run this you see that MATLAB displays x three times, once for each assignment to x inside the loop.
In the first iteration x is a 1-by-1 because we assign to element number 1.
In the second iteration x is a 1-by-3 because we assign to element number 3. MATLAB can't just leave element 2 empty, so it fills it with a default fill value of 0.
In the third iteration x is a 1-by-5 because we assign to element number 5. Again MATLAB can't just leave element 4 empty, so it fills it with 0.
In your code your vector has 50 elements because you assign to element number 50. MATLAB can't leave the elements to which you don't assign empty so it fills them with 0.
"your answer does not help me understand why I receive 50 values instead of 18"
Basically you are doing this:
A(50) = pi
A = 1×50
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
How many elements do you expect an array to have, when you assign something to its 50th element?
"One option is to use nonzeros, but whati if a value that I need is zero?"
As I wrote earlier, just actual indices for indexing, not your data values.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Performance and Memory에 대해 자세히 알아보기

질문:

2022년 12월 8일

편집:

2025년 1월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by