Find row and column of specific array.
4 次查看(过去 30 天)
显示 更早的评论
armin m
2021-11-28
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
采纳的回答
Image Analyst
2021-11-28
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 个评论
armin m
2021-11-28
I have 2016. Q is a matrix that i had maked before. It is not imp9rtant what is Q is!
Image Analyst
2021-11-28
编辑:Image Analyst
2021-11-28
So then, did my solution do what you want?
Can you give a small example?
Maybe you want
out = reshape(Q, [], 2)
armin m
2021-11-28
编辑:Image Analyst
2021-11-28
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
2021-11-28
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.
armin m
2021-11-28
I want a code to do what i said in place of "[Rownum,colnum]=Q(j)" "i know this does not work"
armin m
2021-11-28
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.
Image Analyst
2021-11-28
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
armin m
2021-11-28
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.
Image Analyst
2021-11-28
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
2021-11-28
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.
armin m
2021-11-28
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.
Image Analyst
2021-11-28
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
2021-11-28
编辑:Image Analyst
2021-11-28
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
2021-11-28
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
2021-11-29
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.
armin m
2021-11-29
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!!!
Image Analyst
2021-11-29
@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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)