MATLAB: Numel with loop for+if condition

numel for if

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 ?

Best Answer

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...