Numel with loop for+if condition

17 vues (au cours des 30 derniers jours)
heir ancestors
heir ancestors le 6 Mar 2017
Commenté : heir ancestors le 13 Mar 2017
Hello everybody :
I have the following matrix of 6*6 elements
1 -2 1 1 -3
4 1 2 2 5
7 3 5 4 -7
-8 4 -8 -4 -1
-1 5 -7 7 1
-7 9 4 -2 1
I want to set condition for negative numbers and another condition for positive numbers and calculate a new matrix named B
B=zeros(size(A))
for i=1:numel(A)-1
for j=2:numel(A)
if (A(i)<0)
B(i)=2*sind(A(i))
else
B(i)=1.5*sind(A(i))-2*tand(A(j))
end
end
end
The problem here is that I want to skip the last element of each row I mean :
i is not supposed to reach the last element of each row. it stops always at the before last element, j contrariwise can reach the last element, but the matrix B elements depends on i.
*1* *-2* *1* *1* *-3* ===> not supposed to be reached by *j*
4 1 2 2 5
7 3 5 4 -7
-8 4 -8 -4 -1
-1 5 -7 7 1
*-7* *9* *4* *-2* *1* ===> not supposed to be reached by *i*
I know that numel calculates for all the matrix elements and as I am writing, the code skips only the last element of the matrix and not every last element of a row ; so how to do, please ?

Réponse acceptée

dpb
dpb le 6 Mar 2017
Modifié(e) : dpb le 8 Mar 2017
numel isn't the builtin you're looking for here, it's the product(size(A)) --> 36, not 6.
But, you "don't need no steenkin' loop" anyway; use Matlab vectorized operations--
ADDENDUM/ERRATUM Updated to reflect latest comment re: indexing
OK, for lack of time to consider optimization fully, a working solution using a couple of temporary arrays--
A1=A(1:end-1,:);A2=A(2:end,:); % the two subset sections accounting for offset
ix=A1<0; % as before, the -ive locations in base area
B=zeros(size(ix)); % initialize output to that size
B(ix)=2*sind(A1(ix)); % negative values use same locations only
B(~ix)=1.5*sind(A1(~ix)) ...
-2*tand(A2(~ix)); % +ive also use row below for second term
There's a syntax problem trying to write the subscripting expression using only the one A array that in time I had available couldn't seem to wrap head around for a usable way to write--needed indexing expression that can apply to a result without a temporary a la Octave but not available in Matlab. Came up with same conundrum with either route of trying to shift or use the subarea from the full array for the index so since have other commitments just backed off to the straightforward solution.
To illustrate it operates, here's the result of a test for the above A that shows each of the two values in A being addressed by row for the summing operation without the sind so can pick the locations out easily by eye...
>> [A1(~ix) A2(~ix)]
ans =
1 4
4 7
7 -8
1 3
3 4
4 5
5 9
1 2
2 5
5 -8
1 2
2 4
4 -4
7 -2
5 -7
1 1
>>
This matches the circled locations in your figure...
  15 commentaires
dpb
dpb le 8 Mar 2017
Yeah, I had noticed and fixed that; perhaps overlapped your observing the result.
The 'row/column' thing I understand I think; you were looking at the elements in the vector processing and thinking of them as a "row of numbers" whereas I was trying to interpret which values were intended in the looping construct as the object of the indices and so was needing to know "who's who in the zoo" from a memory/addressing standpoint in the array...
I'm still really snowed; mayhaps later on I can come back and look at the issue of how to write without the temporaries, but unless the actual arrays were to become huge, the penalty here isn't large and you can clear them immediately after use or write a utility function in which they become automagic temporaries which only exist during the lifetime of the function.
heir ancestors
heir ancestors le 13 Mar 2017
Ahein, Thank you again DPB four all the clarifications and all the Matlab tricks : )

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing 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