Get the specific digit of a number

Hello all,
I'm working on an exercise that has multiple parts. At one point of the code, I have to extract all the numbers of a matrix that ends with 3 or 7.
clear all;close all;clc;
load Q4.txt; %Q4 being a matrix of nxm elements with multiple numbers in it.
matrice_Q4 = Q4;
position37 = find(mod(matrice_Q4,10)== 3|7)
val37 = matrice_Q4(position37)
%Tells me what are the numbers in question
What I want is to have the position of all the numbers in the matrix that ends with either 3 or 7.
Thank you very much.

Answers (2)

MATLAB does not allow you to use "==" to compare to two different values at once. Something of the form A==B|C where you want to compare to B or C, can be rewritten as A==B|A==C
dpb
dpb on 10 Nov 2013
Edited: dpb on 10 Nov 2013
Wrong syntax for the combined logical operation and you're looking for what's left over to get the last digit, not the modulo value...
position37 = find(rem(matrice_Q4,10)== 3 | rem(matrice_Q4,10)==7);
Then, to display the results you might find
doc sub2indx
useful or you could use 'logical addressing' to show the positions in a grid if the problem space is small enough for screen space.
ADDENDUM: Actually, either rem or mod works here for the purpose...

1 Comment

Yes, mod() and rem() differ in how they handle negative numbers.

Sign in to comment.

Categories

Asked:

on 10 Nov 2013

Commented:

on 10 Nov 2013

Community Treasure Hunt

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

Start Hunting!