generate a sequence from matrix

given matrix:
new =[
35 28 21 14 7 0
30 23 16 9 2 5
25 18 11 4 3 10
20 13 6 1 8 15
15 8 1 6 13 20
10 3 4 11 18 25
5 2 9 16 23 30
0 7 14 21 28 35]
i need to evaluate from 8th row,1st column.
i need to traverse from 0,2,3,1,1,3,2,0 (least element in each row)
and assign values 1 0 1 1 1 0 1 ( 1 when traversing diagonally and 0 when traversing sideways or upward but not backward or downward)

 Accepted Answer

Interesting...kinda' cute solution...altho note that the suggested answer is wrong; the minimum for row 7 is the 3 in column 2, not 4 in colum 3
[~,j]=min(flipud(new),[],2);
ix=diff(j)~=0;
Above returns
ix =
7×1 logical array
1
0
1
1
1
0
1
>>

5 Comments

thanks a lot friend.
I have a query dpb : what if start the sequence from the second column ignoring the 1st column ?
lets say i have matrix
new =
21 14 7 0
18 11 4 3
15 8 1 6
12 5 2 9
9 2 5 12
6 1 8 15
3 4 11 18
0 7 14 21
i want to ignore the 1st column only and repeat the steps as you mentioned starting from 7th row 2nd column(i.e element 4 ) ?
Just pass the desired subset of the array...
[~,j]=min(flipud(new(:,2:end)),[],2);
ix=diff(j)~=0;
In general, the column expression can be any general subscripting vector desired.
okay,thanks. i have a new issue:
given matrix
a=
16 8 0
13 5 3
10 2 6
7 1 9
4 4 12
1 7 15
2 10 18
5 13 21
when im traversing from 1 to 4 ( as underlined) it should go from 1 to 4 diagonally not vertically(The row above 1 has two 4's, so to avoid conflict i want to move diagonally instead of vertically).The previous code like it assigns 1 for diagonal movement should also work here,because since it goes vertically it gives me 0, whereas i want it to go diagonally,and get 1.
Well, you've just broken any chance for a one-liner I can see... :)
You'll have to do some preprocessing to eliminate the duplicated minima where they exist before calling the above algorithm or have to go back afterwards and check for duplicates and clean up the result if founc.
Or, just loop from the git-go and don't try to be fancy and find each next location one-at-a-time accounting for the duplicates as you go.

Sign in to comment.

More Answers (0)

Asked:

on 29 Jul 2019

Commented:

dpb
on 30 Jul 2019

Community Treasure Hunt

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

Start Hunting!