Effacer les filtres
Effacer les filtres

Cycle for the matrix

1 vue (au cours des 30 derniers jours)
Lev Mihailov
Lev Mihailov le 19 Juin 2019
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 commentaires
Bob Thompson
Bob Thompson le 19 Juin 2019
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);
Lev Mihailov
Lev Mihailov le 19 Juin 2019
Modifié(e) : Lev Mihailov le 19 Juin 2019
Errors in the results shown.
I don’t understand, but where you are going, "g" is exactly what I need for each column.
When i do without cycle :
[M,I] = max(AA);
mn=Wscat((I-100:I),:) ;
v1=mn(mn ~= 0);
[LL,PP] = size(v1);
nmm=Wscat(I:I+LL,:)
v2=nmm(nmm ~= 0);
E1=trapz(v1) ;
E2=trapz(v2) ;
My last values ​​are zeros, although their maxima are in the middle.
Error testimony in "v1",
when looking for maxima and their location of the whole matrix

Connectez-vous pour commenter.

Réponses (3)

Bob Thompson
Bob Thompson le 19 Juin 2019
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 commentaires
Lev Mihailov
Lev Mihailov le 20 Juin 2019
Error on the last "v1" , In an assignment A(:) = B, the number of elements in A and B must be the same.
[d,g] = max(SS,[],1);
% d maximum values, g their location
%% Using max(Wscat,[],1) determines all maximums of each column at the same time
%% Now we need a loop.
for i = 1:length(d)
% creating a matrix from a maximum of -50 to that maximum
if g(i)+LL>size(SS,1)
nmm=SS(g(i):end,i) ;
mn=SS((g(i)-50:g(i)),i)
% creating a matrix from a maximum of -50 to that maximum;
v1(i)=mn(mn ~= 0);
v2(i)=nmm(nmm ~= 0);
elseif g(i)-50<0
mn=SS(1:g(i),i) ;
nmm=SS(g(i):g(i)+LL,i) ;
v1(i)=mn(mn ~= 0);
v2(i)=nmm(nmm ~= 0);
else
nmm=SS(g(i):g(i)+LL,i) ;
mn=SS((g(i)-50:g(i)),i) ;
v1(i)=mn(mn ~= 0); % Error ???
v2(i)=nmm(nmm ~= 0);
end
E1=trapz(v1(i)) ;
E2=trapz(v2(i)) ;
end
fclose(fileID);
Bob Thompson
Bob Thompson le 20 Juin 2019
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);

Connectez-vous pour commenter.


Lev Mihailov
Lev Mihailov le 20 Juin 2019
I replaced v1 and v2 with T and B, but why don't they give out all my columns, but only the last one, and how can I fix this?
[d,g] = max(SS,[],1);
% d maximum values, g their location
%% Using max(Wscat,[],1) determines all maximums of each column at the same time
%% Now we need a loop.
for i = 1:length(d)
% creating a matrix from a maximum of -50 to that maximum
% remove null values
if g(i)+LL>size(SS,1);
nmm=SS(g(i):end,i) ;
mn=SS((g(i)-50:g(i)),i) ;
% creating a matrix from a maximum of -50 to that maximum;
V=mn(mn ~= 0);
T=nmm(nmm ~= 0);
elseif g(i)-50<0
mn=SS(1:g(i),i) ;
nmm=SS(g(i):g(i)+LL,i) ;
V=mn(mn ~= 0);
T=nmm(nmm ~= 0);
else
nmm=SS(g(i):g(i)+LL,i) ;
mn=SS((g(i)-50:g(i)),i) ;
V=mn(mn ~= 0);
T=nmm(nmm ~= 0);
end
E1=trapz(V) ;
E2=trapz(T) ;
end
fclose(fileID);
  1 commentaire
Bob Thompson
Bob Thompson le 20 Juin 2019
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.

Connectez-vous pour commenter.


Lev Mihailov
Lev Mihailov le 21 Juin 2019
[d,g] = max(Wscat,[],1);
LL=size(cell(V)); %% LL - ????
V=cell(1);
T=cell(1);
for i = 1:length(d)
if g(i)+LL>size(Wscat,1); %% LL ????
nmm=Wscat(g(i):end,i) ;
mn=Wscat((g(i)-50:g(i)),i) ;
V{i}=mn(mn ~= 0);
T{i}=nmm(nmm ~= 0);
elseif g(i)-50<0
mn=Wscat(1:g(i),i) ;
nmm=Wscat(g(i):g(i)+LL,i) ;
V{i}=mn(mn ~= 0);
T{i}=nmm(nmm ~= 0);
else
nmm=Wscat(g(i):g(i)+LL,i) ;
mn=Wscat((g(i)-50:g(i)),i) ;
V{i}=mn(mn ~= 0);
T{i}=nmm(nmm ~= 0);
end
E1=trapz(V{i}) ;
E2=trapz(T{i}) ;
end
I need to know V to determine the value of LL, but to check whether the values ​​of the matrix are not exceeded, we use the parameter LL, help get rid of this recruitment
LL=size(cell(V));
I tried to turn the code, but it did not help

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by