How to replace the elements of a Matrix with the elements of a vector

조회 수: 17 (최근 30일)
Marco Boesso
Marco Boesso 2020년 12월 1일
댓글: Marco Boesso 2020년 12월 2일
Good evening, I must perform this task. I will post the code, I hope it is explicative, because it would be a hell of a matter to explain it with my own words:
close all
clear all
clc
A = [1, 2, 4, 7, 11, 16, 22, 29, 37, 46];
delta = A(2:end)-A(1:end-1);
delta(length(delta)+1) = min(delta);
% for those wondering: delta becomes delta = [1 2 3 4 5 6 7 8 9 1]; last element is because I need a tenth element (same size as A) and so I chose the lowest one.
matrice = [0 1 0 1 1 0 0 1 1 1; 1 0 0 0 1 1 1 0 1 1];
[row, col] = find(matrice);
matrice(row, col) = delta(col);
risultato = [0 2 0 4 5 0 0 8 9 1; 1 0 0 0 5 6 7 0 9 1]; % this is what I want the code to do, just don't consider it as a part of the code
So, with this code pasted, I can explain: I want that, while scanning the rows, the matrix element becomes the element in the correspondant column of delta. You find 1 in matrice(1,2) and you replace it with delta(2), for instance.
I will need that to perform the sum along the rows.
The issue is: referring to my example, I want to say "take matrice(1,2) and replace it with delta(2)". MATLAB doesn't speak my language and doesn't read me in my mind (of course, it's a program mounted on something which is wires and binary logic, stupid me! :D), and he understands that I want something else which he can't do. He tells me:
Unable to perform assignment because the size of the left side is 12-by-12 and the size of the right side is 1-by-12.
Error in Sandbox (line 121)
matrice(row, col) = delta(col);
Why don't my good friend MATLAB and I understand each other? What can I do to come an agreement with it? :D
Thanks for your suggestion and I hope that I sound fun enough while you're reading this.
Just two things:
  • don't suggest me solutions where a for-loop is used because who's evaluating my job is very, very annoying. This stuff is going to be inside a for-loop and whenever that person sees a nested for, that person gets a mortal panic attack. I don't want dead people on my heart. Second, the code actually gets slower.
  • I'm using MATLAB r2020, but that person loves old stuff. So I need a solution compatible with r2009 (such a pain in the arse)

채택된 답변

Sibi
Sibi 2020년 12월 1일
편집: Sibi 2020년 12월 1일
A = [1, 2, 4, 7, 11, 16, 22, 29, 37, 46];
delta = A(2:end)-A(1:end-1);
delta(length(delta)+1) = min(delta);
matrice = [0 1 0 1 1 0 0 1 1 1; 1 0 0 0 1 1 1 0 1 1];
output=delta.*(matrice)
  댓글 수: 2
Marco Boesso
Marco Boesso 2020년 12월 1일
Does this work even if the number of rows of "matrice" is indetermined? It may vary, in my code.
The number of columns, of course, will remain fixed
Marco Boesso
Marco Boesso 2020년 12월 2일
Thank you! I tried your code out and it works! Brilliant!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Signal Attributes and Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by