Find row and column of specific array.

Hi. I want to find row and column of array in matrix. But with out using the find function. Because it finds the same numbers and put them in specific matrix. I want the code like this:
C=0
for j=1:10
C=C+1
[Rownum, colnum]=Q(j)
R(C,1)=Rownum
R(C,2)=colnum
end

 Accepted Answer

What is Q?
To get the rows and columns try the meshgrid() function:
x = 1 : 5; % 5 columns.
y = 1 : 4; % 4 rows.
[columnsArray, rowsArray] = meshgrid(x, y)
columnsArray = 4×5
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
rowsArray = 4×5
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4

24 Comments

I have 2016. Q is a matrix that i had maked before. It is not imp9rtant what is Q is!
So then, did my solution do what you want?
Can you give a small example?
Maybe you want
out = reshape(Q, [], 2)
I explained first
Q=[5 7 8 9 0 7 6 7 88]
C=0
for j=1:10
if Q(j)~=0
C=C+1
[Rownum,colnum]=Q(j)
R(C,1)=Rownum
R(C,2)=colnum
end
end
If i want explain simply, i want make matrix of row and column of array in specific arrangment of j=1:10
I still don't understand when you say
[Rownum,colnum]=Q(j)
That won't work. Q(j) returns a single number, not two numbers.
thisValue = Q(j); % Works.
Ha ha . I mean a code to replace with code that you do not understand
I want a code to do what i said in place of "[Rownum,colnum]=Q(j)" "i know this does not work"
For e.g . In run mode: j=4 Q(4)=9 C=4 Row and column > [1,4] So R(4,1)=1 R(4,2)=4 If i wanna say simply. I wanna a code which take row and column of Q(j) in each iteration.
Why do you not want to use the find function? Do you want to do this
C=0
R = zeros(size(Q, 1), 2);
for j = 1 : length(Q)
if Q(j) == j
C=C+1
Rownum = 1;
colnum = j;
R(C,1)=Rownum;
R(C,2)=colnum;
end
end
Beacause it take vector instead of number if it find same numbers. For example i takes Row 1 1 1 and col 2 6 8 for number 7.
Please give your expected outputs Rownum and colnum so I can try to figure out what you want, because your explanations are just not getting through to me.
armin m
armin m on 28 Nov 2021
Edited: armin m on 28 Nov 2021
No it does not works beacause if matrix has mor than 1 row it does not works

Your code is wrong though

Thank you for spending time. I have a matrix which has many zero array. I want find non zero array and put their row and column in specific matrixes. May be i have weakness in explanation but ot is first time i have difficulty in problem explanation.
I have this matrix Q=[1,3,5;7,8,9;8,77,0;8,0,9] I want to put row of non 0 arrays in first column of Matrix R and column of these arrays in second column of matrix R.
It's really hard working with you (I'm about to give up).
OK, please say what you expect R to be (you forgot to give your desired output).
armin m
armin m on 28 Nov 2021
Edited: armin m on 28 Nov 2021

Q=[1,3,5;7,8,9;8,77,0;8,0,9]

Q11=[1,3,5,7,8,9,8,77,8,9]

R=[1,1;1,2;1,3;2,1;2,2;2,3;3,1;3,2;4,1;4,3]

I first started to do it vectorized but because you're arranging things and ordering things in a non-standard order it just because so cryptic and complicated with transposing things, etc. that I decided to do it with a simple, intuitive and eacsy to follow for loop:
Q=[1,3,5;7,8,9;8,77,0;8,0,9]
Q = 4×3
1 3 5 7 8 9 8 77 0 8 0 9
% R=[1,1;1,2;1,3;2,1;2,2;2,3;3,1;3,2;4,1;4,3] % Desired output.
% Get size of Q.
[rows, columns] = size(Q)
rows = 4
columns = 3
counter = 1;
for row = 1 : rows
for col = 1 : columns
if Q(row, col) ~= 0
R(counter, 1) = row;
R(counter, 2) = col;
counter = counter + 1;
end
end
end
R
R = 10×2
1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 4 1 4 3
It gives you the desired output for the one example you gave. So is there anything still wrong with it, such that you haven't Accepted the answer yet? Tell me what it is and I'll fix it.
armin m
armin m on 28 Nov 2021
Edited: armin m on 28 Nov 2021
No it was the best answer. Do you know how much matlab program writing costs in engeneerig? Or matlab have site that sell codes or some thing like that?!
Since you say it was the best answer, could you please click the "Accept this answer" link? Thanks in advance.
The cost of writing MATLAB code varies a lot. About 10 years ago we asked the Mathworks and their rate then was $190 per hour. You can get computer programmers to write code for around $90 - $150 per hour here in the US but that's just in general for languages like C and Python and Java. We did hire one local engineering firm that did MATLAB programming for us at around $90 per hour but then I think their latest price was more like $130 per hour.
You can try fiverr.com and get freelance MATLAB programmers for very much cheaper, though if their quality is like what I see here by questioners in the Answers forum, it's not going to be professional quality.
I asked it because i write program code for some one . It is for electrical engeneering. I wanna know how much should i take from him!!!
@armin m, I don't know what the going rate is for custom programming in your country is.
"i write" doesn't make too much sense. It would be either
  1. "I am writing" meaning you have already begun and are currently writing for him.
  2. "I wrote" meaning you finished writing and are all done.
  3. "I will write" meaning you have not yet started to write but will write for him in the future.
As you can imagine, the hourly rate varies a lot from country to country. Usually you'll give them a cost estimate before you begin work so you also have to estimate the time it will take you to do the job.
I am writing

Sign in to comment.

More Answers (0)

Asked:

on 28 Nov 2021

Commented:

on 29 Nov 2021

Community Treasure Hunt

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

Start Hunting!