Cycle for the matrix
2 次查看(过去 30 天)
显示 更早的评论
Hello! I have a matrix with dimensions of 600 per 1000, it is necessary to select a maximum of each column and count the first 50 to the maximum, without taking into account zero values and the first 50 after it, also without zero values
here is my code:
SS=AA(1:600,:);
Lum=1;
a1 = SS(:,Lum);
[d,g] = max(a1);
% d maximum values, g their location
mn=a1((g-50:g),:) ;
% creating a matrix from a maximum of -50 to that maximum
v1=mn(mn ~= 0);
% remove null values
[LL,PP] = size(v1);
%how many were not zero elements
nmm=a1(g:g+LL,:) ;
% from the maximum + how many elements were in the first part
v2=nmm(nmm ~= 0); % удаляем нулевые значения
% remove null values
E1=trapz(v1) ;
E2=trapz(v2) ;
% integrals 1 and 2 parts
"Lum" is one of my entries, I can count everything, but when setting up the "for" loop where Lum from 1 to 1000 gives me an error, help me figure it out.
2 个评论
Bob Thompson
2019-6-19
What error did you receive? Please copy the entire error message.
Also, you can use max() to do the maximums of all columns at the same time.
SS=AA(1:600,:);
d = max(SS,[],1);
回答(3 个)
Bob Thompson
2019-6-19
Try this instead. It calculates all of the maximums at the same time and then loops through the results. I wasn't able to confirm that it worked because I ran into an error where the maximum value was too close to the beginning or end of the column, but that was because I used a bunch of random numbers. You may need to make some other minor modifications to this.
SS=AA(1:600,:);
[d,g] = max(SS,[],1);
% d maximum values, g their location
%% Using max(SS,[],1) determines all maximums of each column at the same time
%% Now we need a loop.
for i = 1:length(d)
mn=SS((g(i)-50:g(i)),i) ;
% creating a matrix from a maximum of -50 to that maximum
v1=mn(mn ~= 0);
% remove null values
[LL,PP] = size(v1);
%how many were not zero elements
nmm=SS(g(i):g(i)+LL,i) ;
% from the maximum + how many elements were in the first part
v2=nmm(nmm ~= 0); % удаляем нулевые значения
% remove null values
E1=trapz(v1) ;
E2=trapz(v2) ;
% integrals 1 and 2 parts
end
4 个评论
Bob Thompson
2019-6-20
Yes, you're going to get that error because you're trying to put an array into a single element. You either need to just do v1, v2, or to change the class of v1 and v2 and make them cells. If you want to do the entire array, then just get rid of the indexing (you won't capture the different values for use outside the loop though), and if you want to do the cells method then change all of the v1 indexing marks to use curly braces.
% Array method
v1 = mn(mn ~= 0);
% Cell method
v1{i} = mn(mn ~= 0);
Lev Mihailov
2019-6-20
1 个评论
Bob Thompson
2019-6-20
As I mentioned previously, though not as explicitly, removing the indexing from v1 and v2 allows you to overwrite the entire matrix with mn or nmm. Overwriting the results removes the previous results, so the only visible outcome is the final column, as you say. If you would like to capture all results, I would suggest indexing with cells, because you can't affirm that the results from each column will be the same size.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!