You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Find row and column of specific array.
4 views (last 30 days)
Show older comments
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
Image Analyst
on 28 Nov 2021
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)
24 Comments
Image Analyst
on 28 Nov 2021
Edited: Image Analyst
on 28 Nov 2021
So then, did my solution do what you want?
Can you give a small example?
Maybe you want
out = reshape(Q, [], 2)
armin m
on 28 Nov 2021
Edited: Image Analyst
on 28 Nov 2021
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
Image Analyst
on 28 Nov 2021
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.
Image Analyst
on 28 Nov 2021
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
Image Analyst
on 28 Nov 2021
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
on 28 Nov 2021
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.
Image Analyst
on 28 Nov 2021
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).
Image Analyst
on 28 Nov 2021
Edited: Image Analyst
on 28 Nov 2021
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
Image Analyst
on 28 Nov 2021
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.
Image Analyst
on 29 Nov 2021
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.
Image Analyst
on 29 Nov 2021
@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
- "I am writing" meaning you have already begun and are currently writing for him.
- "I wrote" meaning you finished writing and are all done.
- "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.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)